feat(api): add subscribe to quincy email endpoint (#50305)

This commit is contained in:
Muhammed Mustafa
2023-05-09 12:10:39 +03:00
committed by GitHub
parent a267f5a6d9
commit afd4402a49
2 changed files with 66 additions and 0 deletions

View File

@@ -159,6 +159,31 @@ describe('settingRoutes', () => {
expect(response?.statusCode).toEqual(400);
});
});
describe('/update-my-quincy-email', () => {
test('PUT returns 200 status code with "success" message', async () => {
const response = await request(fastify?.server)
.put('/update-my-quincy-email')
.set('Cookie', cookies)
.send({ sendQuincyEmail: true });
expect(response?.statusCode).toEqual(200);
expect(response?.body).toEqual({
message: 'flash.subscribe-to-quincy-updated',
type: 'success'
});
});
test('PUT returns 400 status code with invalid sendQuincyEmail', async () => {
const response = await request(fastify?.server)
.put('/update-my-quincy-email')
.set('Cookie', cookies)
.send({ sendQuincyEmail: 'invalid' });
expect(response?.statusCode).toEqual(400);
});
});
});
describe('Unauthenticated User', () => {

View File

@@ -152,5 +152,46 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
}
}
);
fastify.put(
'/update-my-quincy-email',
{
schema: {
body: Type.Object({
sendQuincyEmail: Type.Boolean()
}),
response: {
200: Type.Object({
message: Type.Literal('flash.subscribe-to-quincy-updated'),
type: Type.Literal('success')
}),
500: Type.Object({
message: Type.Literal('flash.wrong-updating'),
type: Type.Literal('danger')
})
}
}
},
async (req, reply) => {
try {
await fastify.prisma.user.update({
where: { id: req.session.user.id },
data: {
sendQuincyEmail: req.body.sendQuincyEmail
}
});
return {
message: 'flash.subscribe-to-quincy-updated',
type: 'success'
} as const;
} catch (err) {
fastify.log.error(err);
void reply.code(500);
return { message: 'flash.wrong-updating', type: 'danger' } as const;
}
}
);
done();
};