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'
7 function checkRegistrationIdExist (idArg: number | string, res: express.Response) {
8 const id = forceNumber(idArg)
9 return checkRegistrationExist(() => UserRegistrationModel.load(id), res)
12 function checkRegistrationEmailExist (email: string, res: express.Response, abortResponse = true) {
13 return checkRegistrationExist(() => UserRegistrationModel.loadByEmail(email), res, abortResponse)
16 async function checkRegistrationHandlesDoNotAlreadyExist (options: {
22 const { res } = options
24 const registration = await UserRegistrationModel.loadByEmailOrHandle(pick(options, [ 'username', 'email', 'channelHandle' ]))
28 status: HttpStatusCode.CONFLICT_409,
29 message: 'Registration with this username, channel name or email already exists.'
37 async function checkRegistrationExist (finder: () => Promise<MRegistration>, res: express.Response, abortResponse = true) {
38 const registration = await finder()
41 if (abortResponse === true) {
43 status: HttpStatusCode.NOT_FOUND_404,
44 message: 'User not found'
51 res.locals.userRegistration = registration
56 checkRegistrationIdExist,
57 checkRegistrationEmailExist,
58 checkRegistrationHandlesDoNotAlreadyExist,
59 checkRegistrationExist