2023-06-14 13:56:47 +00:00
|
|
|
import UserEmail from 'Model/Models/UserEmail';
|
|
|
|
import UserEmailService, {
|
|
|
|
Service as UserEmailServiceType,
|
|
|
|
} from '../Services/UserEmailService';
|
|
|
|
import BaseAPI from './BaseAPI';
|
2023-06-15 14:07:34 +00:00
|
|
|
import {
|
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
OneUptimeRequest,
|
|
|
|
} from '../Utils/Express';
|
|
|
|
import UserMiddleware from '../Middleware/UserAuthorization';
|
|
|
|
import BadDataException from 'Common/Types/Exception/BadDataException';
|
|
|
|
import Response from '../Utils/Response';
|
2023-06-14 13:56:47 +00:00
|
|
|
|
2023-06-14 19:13:24 +00:00
|
|
|
export default class UserEmailAPI extends BaseAPI<
|
|
|
|
UserEmail,
|
|
|
|
UserEmailServiceType
|
|
|
|
> {
|
2023-06-14 13:56:47 +00:00
|
|
|
public constructor() {
|
|
|
|
super(UserEmail, UserEmailService);
|
2023-06-15 14:07:34 +00:00
|
|
|
|
|
|
|
this.router.post(
|
|
|
|
`${new this.entityType().getCrudApiPath()?.toString()}/verify`,
|
|
|
|
UserMiddleware.getUserMiddleware,
|
|
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
req = req as OneUptimeRequest;
|
|
|
|
|
|
|
|
if (!req.body.itemId) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Invalid item ID')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.body.code) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Invalid code')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the code matches and verify the email.
|
2023-06-16 11:33:16 +00:00
|
|
|
const item: UserEmail | null = await this.service.findOneById({
|
2023-06-15 14:07:34 +00:00
|
|
|
id: req.body['itemId'],
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
verificationCode: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Item not found')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-30 15:30:15 +00:00
|
|
|
//check user id
|
2023-06-15 14:07:34 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
item.userId?.toString() !==
|
|
|
|
(
|
|
|
|
req as OneUptimeRequest
|
|
|
|
)?.userAuthorization?.userId?.toString()
|
|
|
|
) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Invalid user ID')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.verificationCode !== req.body['code']) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Invalid code')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.service.updateOneById({
|
|
|
|
id: item.id!,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
isVerified: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return Response.sendEmptyResponse(req, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.router.post(
|
|
|
|
`${new this.entityType()
|
|
|
|
.getCrudApiPath()
|
|
|
|
?.toString()}/resend-verification-code`,
|
|
|
|
UserMiddleware.getUserMiddleware,
|
|
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
req = req as OneUptimeRequest;
|
|
|
|
|
|
|
|
if (!req.body.itemId) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('Invalid item ID')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-16 11:33:16 +00:00
|
|
|
await this.service.resendVerificationCode(req.body.itemId);
|
2023-06-15 14:07:34 +00:00
|
|
|
|
|
|
|
return Response.sendEmptyResponse(req, res);
|
|
|
|
}
|
|
|
|
);
|
2023-06-14 13:56:47 +00:00
|
|
|
}
|
|
|
|
}
|