2024-06-14 11:09:53 +00:00
|
|
|
import UserMiddleware from "../Middleware/UserAuthorization";
|
2023-06-14 13:56:47 +00:00
|
|
|
import UserCallService, {
|
2024-06-14 11:09:53 +00:00
|
|
|
Service as UserCallServiceType,
|
|
|
|
} from "../Services/UserCallService";
|
2023-06-16 12:26:32 +00:00
|
|
|
import {
|
2024-06-14 11:09:53 +00:00
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
OneUptimeRequest,
|
|
|
|
} from "../Utils/Express";
|
|
|
|
import Response from "../Utils/Response";
|
|
|
|
import BaseAPI from "./BaseAPI";
|
|
|
|
import BadDataException from "Common/Types/Exception/BadDataException";
|
|
|
|
import UserCall from "Model/Models/UserCall";
|
|
|
|
import UserSMS from "Model/Models/UserSMS";
|
2023-06-14 13:56:47 +00:00
|
|
|
|
2023-06-14 19:13:24 +00:00
|
|
|
export default class UserCallAPI extends BaseAPI<
|
2024-06-14 11:09:53 +00:00
|
|
|
UserCall,
|
|
|
|
UserCallServiceType
|
2023-06-14 19:13:24 +00:00
|
|
|
> {
|
2024-06-14 11:09:53 +00:00
|
|
|
public constructor() {
|
|
|
|
super(UserCall, UserCallService);
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
this.router.post(
|
|
|
|
`/user-call/verify`,
|
|
|
|
UserMiddleware.getUserMiddleware,
|
|
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
req = req as OneUptimeRequest;
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!req.body.itemId) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Invalid item ID"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!req.body.code) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Invalid code"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// Check if the code matches and verify the phone number.
|
|
|
|
const item: UserSMS | null = await this.service.findOneById({
|
|
|
|
id: req.body["itemId"],
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
verificationCode: true,
|
|
|
|
},
|
|
|
|
});
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!item) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Item not found"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
//check user id
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (
|
|
|
|
item.userId?.toString() !==
|
|
|
|
(req as OneUptimeRequest)?.userAuthorization?.userId?.toString()
|
|
|
|
) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Invalid user ID"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (item.verificationCode !== req.body["code"]) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Invalid code"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
await this.service.updateOneById({
|
|
|
|
id: item.id!,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
isVerified: true,
|
|
|
|
},
|
|
|
|
});
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
return Response.sendEmptySuccessResponse(req, res);
|
|
|
|
},
|
|
|
|
);
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
this.router.post(
|
|
|
|
`/user-call/resend-verification-code`,
|
|
|
|
UserMiddleware.getUserMiddleware,
|
|
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
req = req as OneUptimeRequest;
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!req.body.itemId) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException("Invalid item ID"),
|
|
|
|
);
|
|
|
|
}
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
await this.service.resendVerificationCode(req.body.itemId);
|
2023-06-16 12:26:32 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
return Response.sendEmptySuccessResponse(req, res);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-06-14 13:56:47 +00:00
|
|
|
}
|