109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
import {CUSTOMER_WISHLIST_READ_QUERY} from '~/graphql/customer-account/CustomerWishlistReadQuery';
|
|
import {WISHLIST_METAFIELD_SET_MUTATION} from '~/graphql/customer-account/WishlistMetafieldSetMutation';
|
|
import {parseWishlistHandlesFromBody} from '~/lib/abiirWishlistStorage';
|
|
import type {Route} from '~/lib/reactRouterRouteStubs';
|
|
|
|
/**
|
|
* Merges browser wishlist (FormData `handles` JSON) with server customer metafield
|
|
* and persists via `metafieldsSet` when the shop is configured. Response always
|
|
* includes `merged` for the client to `writeWishlistHandles`.
|
|
*/
|
|
export async function loader() {
|
|
return new Response(null, {status: 404});
|
|
}
|
|
|
|
export async function action({request, context}: Route.ActionArgs) {
|
|
if (request.method !== 'POST') {
|
|
return Response.json({error: 'method_not_allowed'}, {status: 405});
|
|
}
|
|
const {customerAccount} = context;
|
|
if (!(await customerAccount.isLoggedIn())) {
|
|
return Response.json({error: 'unauthorized'}, {status: 401});
|
|
}
|
|
|
|
const form = await request.formData();
|
|
let local: string[] = [];
|
|
try {
|
|
local = parseWishlistHandlesFromBody(
|
|
JSON.parse(String(form.get('handles') ?? '[]')),
|
|
);
|
|
} catch {
|
|
return Response.json({error: 'invalid_handles'}, {status: 400});
|
|
}
|
|
|
|
const {data, errors} = await customerAccount.query(
|
|
CUSTOMER_WISHLIST_READ_QUERY,
|
|
{
|
|
variables: {language: customerAccount.i18n.language},
|
|
},
|
|
);
|
|
|
|
if (errors?.length || !data?.customer?.id) {
|
|
return Response.json({
|
|
ok: false,
|
|
merged: local,
|
|
sync: 'local_only' as const,
|
|
});
|
|
}
|
|
|
|
const customerId = data.customer.id;
|
|
let server: string[] = [];
|
|
const rawMf = data.customer.metafield?.value;
|
|
if (rawMf) {
|
|
try {
|
|
server = parseWishlistHandlesFromBody(JSON.parse(String(rawMf)));
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
const merged = parseWishlistHandlesFromBody([...local, ...server]);
|
|
const compareDigest = data.customer.metafield?.compareDigest;
|
|
|
|
const metafieldInput: {
|
|
ownerId: string;
|
|
namespace: string;
|
|
key: string;
|
|
value: string;
|
|
type: string;
|
|
compareDigest?: string | null;
|
|
} = {
|
|
ownerId: customerId,
|
|
namespace: 'custom',
|
|
key: 'abiir_wishlist',
|
|
value: JSON.stringify(merged),
|
|
type: 'json',
|
|
};
|
|
if (typeof compareDigest === 'string') {
|
|
metafieldInput.compareDigest = compareDigest;
|
|
}
|
|
|
|
const {data: mData, errors: mErrors} = await customerAccount.mutate(
|
|
WISHLIST_METAFIELD_SET_MUTATION,
|
|
{
|
|
variables: {
|
|
language: customerAccount.i18n.language,
|
|
metafields: [metafieldInput],
|
|
},
|
|
},
|
|
);
|
|
|
|
const userErrors = mData?.metafieldsSet?.userErrors ?? [];
|
|
if (mErrors?.length || userErrors.length) {
|
|
return Response.json({
|
|
ok: true,
|
|
merged,
|
|
sync: 'server_write_skipped' as const,
|
|
message:
|
|
userErrors[0]?.message ??
|
|
mErrors?.[0]?.message ??
|
|
'metafield_write_failed',
|
|
});
|
|
}
|
|
|
|
return Response.json({
|
|
ok: true,
|
|
merged,
|
|
sync: 'server' as const,
|
|
});
|
|
}
|