@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
131 lines (130 loc) • 4.29 kB
JavaScript
import { ErrorResponse } from "../../../errors/index.mjs";
import { CustomerAPIClient } from "../../../api/customer.mjs";
import { getOAuthClient } from "../../../api/oauth.mjs";
import { FetchError } from "../../../utils/fetch.mjs";
import {
HttpStatusCode,
HttpStatusMessage
} from "../../../constants/httpStatus.mjs";
import { defineRpcHandler } from "../../../utils/index.mjs";
export const updateShopUser = defineRpcHandler(
async (payload, context) => {
const shopId = context.shopId;
const updatedUser = {
email: payload.email,
phone: payload.phone,
firstName: payload.firstName,
lastName: payload.lastName,
birthDate: payload.birthDate,
gender: payload.gender,
title: payload.title
};
if (!context.user) {
const message = "Unauthorized request: Missing access token";
context.log.error(message);
return new ErrorResponse(
HttpStatusCode.UNAUTHORIZED,
HttpStatusMessage.UNAUTHORIZED,
"No access token present"
);
}
const user = {
...context.user,
...updatedUser
};
const client = new CustomerAPIClient(context);
try {
await Promise.all([
client.updateContactInfo(shopId, {
email: user.email,
phone: user.phone
}),
client.updatePersonalInfo(shopId, {
firstName: user?.firstName,
lastName: user?.lastName,
birthDate: user?.birthDate,
...user?.gender && { gender: user?.gender },
...user?.title && { title: user?.title }
})
]);
context.updateUser(user);
return { user };
} catch (error) {
context.log.error("Error while updating user information", error);
return new ErrorResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR,
HttpStatusMessage.INTERNAL_SERVER_ERROR,
"Error while updating user information"
);
}
},
{ method: "PUT" }
);
export const updatePassword = defineRpcHandler(
async ({ oldPassword, newPassword }, context) => {
const shopUser = context.user;
const oauthEnabled = context.oauth?.apiHost && context.oauth?.clientId && context.oauth?.clientSecret;
try {
if (oauthEnabled) {
const client2 = getOAuthClient(context);
if (!context.accessToken) {
return new ErrorResponse(
HttpStatusCode.UNAUTHORIZED,
HttpStatusMessage.UNAUTHORIZED,
"No access token present"
);
}
await client2.updatePassword(
{
password: oldPassword,
new_password: newPassword
},
context.accessToken
);
return { user: shopUser };
}
const client = new CustomerAPIClient(context);
const user = await client.updatePassword(context.shopId, {
password: oldPassword,
newPassword
});
if (shopUser?.id) {
await context.destroySessionsForUserId(shopUser.id, [context.sessionId]);
}
return { user };
} catch (error) {
if (!(error instanceof FetchError)) {
context.log.error("Error while updating user's password", error);
return new ErrorResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR,
HttpStatusMessage.INTERNAL_SERVER_ERROR,
"Error while updating user's password"
);
}
if (error.response.status === HttpStatusCode.UNAUTHORIZED) {
return new ErrorResponse(
HttpStatusCode.UNAUTHORIZED,
HttpStatusMessage.UNAUTHORIZED,
"Failed to update user's password",
{ detail: "Unauthorized request" }
);
} else if (error.response.status === HttpStatusCode.FORBIDDEN) {
return new ErrorResponse(
HttpStatusCode.FORBIDDEN,
HttpStatusMessage.FORBIDDEN,
"Failed to update user's password",
{ detail: "Invalid auth" }
);
} else if (error.response.status === HttpStatusCode.NOT_FOUND) {
return new ErrorResponse(
HttpStatusCode.NOT_FOUND,
HttpStatusMessage.NOT_FOUND,
"Failed to update user's password",
{ detail: "User not found" }
);
}
}
return { user: shopUser };
},
{ method: "PUT" }
);