]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { UserRegistrationModel } from '@server/models/user/user-registration' | |
3 | import { MRegistration } from '@server/types/models' | |
4 | import { forceNumber, pick } from '@shared/core-utils' | |
5 | import { HttpStatusCode } from '@shared/models' | |
6 | ||
7 | function checkRegistrationIdExist (idArg: number | string, res: express.Response) { | |
8 | const id = forceNumber(idArg) | |
9 | return checkRegistrationExist(() => UserRegistrationModel.load(id), res) | |
10 | } | |
11 | ||
12 | function checkRegistrationEmailExist (email: string, res: express.Response, abortResponse = true) { | |
13 | return checkRegistrationExist(() => UserRegistrationModel.loadByEmail(email), res, abortResponse) | |
14 | } | |
15 | ||
16 | async function checkRegistrationHandlesDoNotAlreadyExist (options: { | |
17 | username: string | |
18 | channelHandle: string | |
19 | email: string | |
20 | res: express.Response | |
21 | }) { | |
22 | const { res } = options | |
23 | ||
24 | const registration = await UserRegistrationModel.loadByEmailOrHandle(pick(options, [ 'username', 'email', 'channelHandle' ])) | |
25 | ||
26 | if (registration) { | |
27 | res.fail({ | |
28 | status: HttpStatusCode.CONFLICT_409, | |
29 | message: 'Registration with this username, channel name or email already exists.' | |
30 | }) | |
31 | return false | |
32 | } | |
33 | ||
34 | return true | |
35 | } | |
36 | ||
37 | async function checkRegistrationExist (finder: () => Promise<MRegistration>, res: express.Response, abortResponse = true) { | |
38 | const registration = await finder() | |
39 | ||
40 | if (!registration) { | |
41 | if (abortResponse === true) { | |
42 | res.fail({ | |
43 | status: HttpStatusCode.NOT_FOUND_404, | |
44 | message: 'User not found' | |
45 | }) | |
46 | } | |
47 | ||
48 | return false | |
49 | } | |
50 | ||
51 | res.locals.userRegistration = registration | |
52 | return true | |
53 | } | |
54 | ||
55 | export { | |
56 | checkRegistrationIdExist, | |
57 | checkRegistrationEmailExist, | |
58 | checkRegistrationHandlesDoNotAlreadyExist, | |
59 | checkRegistrationExist | |
60 | } |