49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
'use server';
|
|
|
|
import { redirect } from 'next/navigation';
|
|
|
|
import { updateCustomer } from '@lib/data/customer';
|
|
|
|
import { AccountSubmitData, createAuthApi } from '@kit/auth/api';
|
|
import { enhanceAction } from '@kit/next/actions';
|
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
|
|
|
import pathsConfig from '~/config/paths.config';
|
|
|
|
import { UpdateAccountSchema } from '../schemas/update-account.schema';
|
|
|
|
export const onUpdateAccount = enhanceAction(
|
|
async (params: AccountSubmitData) => {
|
|
const client = getSupabaseServerClient();
|
|
const api = createAuthApi(client);
|
|
|
|
try {
|
|
await api.updateAccount(params);
|
|
console.log('SUCCESS', pathsConfig.auth.updateAccountSuccess);
|
|
} catch (err: unknown) {
|
|
if (err instanceof Error) {
|
|
console.warn('On update account error: ' + err.message);
|
|
}
|
|
console.warn('On update account error: ', err);
|
|
}
|
|
|
|
await updateCustomer({
|
|
first_name: params.firstName,
|
|
last_name: params.lastName,
|
|
phone: params.phone,
|
|
});
|
|
|
|
const hasUnseenMembershipConfirmation =
|
|
await api.hasUnseenMembershipConfirmation();
|
|
|
|
if (hasUnseenMembershipConfirmation) {
|
|
redirect(pathsConfig.auth.membershipConfirmation);
|
|
} else {
|
|
redirect(pathsConfig.app.selectPackage);
|
|
}
|
|
},
|
|
{
|
|
schema: UpdateAccountSchema,
|
|
},
|
|
);
|