-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticketsActions.ts
83 lines (77 loc) · 2.07 KB
/
ticketsActions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {FastifyInstance, FastifyReply, FastifyRequest} from 'fastify';
import z from 'zod';
import {EMAIL_TEMPLATE, errorResponseSchema} from '../constant';
import {verifyTicket} from '../services/commonApi';
import {createSingleTicket} from '../services/ticketsService';
import {DbService} from '../_core/services/dbService';
import {Action} from '../_core/type';
export const createTicket: Action = {
path: '/',
method: 'post',
options: {
schema: {
body: z.object({
chain: z.coerce.number(),
email: z.string().email(),
}),
response: {
201: z.object({ticketId: z.string()}),
400: errorResponseSchema,
},
security: [{jwt: []}],
},
},
handler: createTicketHandler,
};
async function createTicketHandler(
this: FastifyInstance,
request: FastifyRequest,
reply: FastifyReply
) {
const dbService: DbService = this.diContainer.resolve('dbService');
const {chain, email: rawEmail} = request.body as any;
try {
const ticketId = await createSingleTicket(dbService, chain, rawEmail, {
subject: EMAIL_TEMPLATE.subject,
templateUrl: EMAIL_TEMPLATE.url,
});
return reply.status(201).send({ticketId});
} catch (e: any) {
return reply.status(400).send({
error: e.message,
});
}
}
export const claimReward: Action = {
path: '/',
method: 'post',
options: {
schema: {
body: z.object({
signedToken: z.coerce.string(),
}),
response: {
201: z.object({ticketId: z.string()}),
400: errorResponseSchema,
},
security: [{jwt: []}],
},
},
handler: claimRewardHandler,
};
async function claimRewardHandler(
this: FastifyInstance,
request: FastifyRequest,
reply: FastifyReply
) {
// const dbService: DbService = this.diContainer.resolve('dbService');
const {signedToken} = request.body as any;
let result = {};
try {
result = await verifyTicket(signedToken);
// verify passed!
return reply.status(200).send(result);
} catch (e) {
return reply.status(400).send({error: 'invalid signedToken!'});
}
}