2021-09-24 23:09:20 +00:00
|
|
|
import { NotFoundError, resolver } from "blitz";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import db from "../../../db";
|
|
|
|
import notifyEmailChangeQueue from "../api/queue/notify-email-change";
|
|
|
|
|
|
|
|
const Body = z.object({
|
|
|
|
email: z.string().email(),
|
2021-09-25 14:16:31 +00:00
|
|
|
fullName: z.string(),
|
2021-09-24 23:09:20 +00:00
|
|
|
});
|
|
|
|
|
2021-09-25 14:16:31 +00:00
|
|
|
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ email, fullName }, ctx) => {
|
2021-09-24 23:09:20 +00:00
|
|
|
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } });
|
|
|
|
if (!user) throw new NotFoundError();
|
|
|
|
|
|
|
|
const oldEmail = user.email;
|
|
|
|
await db.user.update({
|
|
|
|
where: { id: user.id },
|
2021-09-25 14:16:31 +00:00
|
|
|
data: { email, fullName },
|
2021-09-24 23:09:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (oldEmail !== email) {
|
|
|
|
// await notifyEmailChangeQueue.enqueue({ newEmail: email, oldEmail: user.email });
|
|
|
|
}
|
|
|
|
});
|