UNPKG

@brewww/authentication-service

Version:
82 lines 4.41 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendSignUpOtpPhoneController = void 0; const openapi = require("@nestjs/swagger"); const common_1 = require("@nestjs/common"); const user_service_1 = require("../user/user.service"); const swagger_1 = require("@nestjs/swagger"); const otp_service_1 = require("../otp/otp.service"); const event_emitter_1 = require("@nestjs/event-emitter"); const enum_1 = require("../enum"); const error_1 = require("../error"); const send_sign_up_otp_phone_request_dto_1 = require("./dto/send-sign-up-otp-phone-request.dto"); let SendSignUpOtpPhoneController = class SendSignUpOtpPhoneController { constructor(userService, otpService, eventEmitter) { this.userService = userService; this.otpService = otpService; this.eventEmitter = eventEmitter; } async sendSignUpOtpPhoneAsync(sendSignUpOtpPhoneRequest) { try { const user = await this.userService.getUserAsync({ phone: sendSignUpOtpPhoneRequest.phone, }); if (user) throw new error_1.UserAlreadyExistsError(); const sendOtpResult = await this.otpService.createPhoneOtpAsync({ phone_number: sendSignUpOtpPhoneRequest.phone.number, country_code: sendSignUpOtpPhoneRequest.phone.countryCode, }); if (sendOtpResult.isSent === true) { this.eventEmitter.emit("otp.sms.created", { otpValue: sendOtpResult.otpValue, phoneNumber: sendSignUpOtpPhoneRequest.phone.countryCode + sendSignUpOtpPhoneRequest.phone.number, authenticationAction: enum_1.AuthenticationAction.SIGNUP, locale: sendSignUpOtpPhoneRequest === null || sendSignUpOtpPhoneRequest === void 0 ? void 0 : sendSignUpOtpPhoneRequest.locale, appData: sendSignUpOtpPhoneRequest === null || sendSignUpOtpPhoneRequest === void 0 ? void 0 : sendSignUpOtpPhoneRequest.appData, }); } return { isSent: sendOtpResult.isSent, expiresAt: sendOtpResult.expiresAt, }; } catch (error) { if (error instanceof error_1.UserAlreadyExistsError) throw new common_1.ConflictException(null, { cause: error }); throw new common_1.InternalServerErrorException(); } } }; __decorate([ (0, common_1.Post)("send-sign-up-otp-phone"), openapi.ApiResponse({ status: 201, type: require("./dto/send-sign-up-otp-phone-response.dto").SendSignUpOtpPhoneResponse }), __param(0, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [send_sign_up_otp_phone_request_dto_1.SendSignUpOtpPhoneRequest]), __metadata("design:returntype", Promise) ], SendSignUpOtpPhoneController.prototype, "sendSignUpOtpPhoneAsync", null); SendSignUpOtpPhoneController = __decorate([ (0, swagger_1.ApiTags)("authentication"), (0, common_1.Controller)(), (0, swagger_1.ApiSecurity)("ApiKey"), __param(0, (0, common_1.Inject)("UserService")), __metadata("design:paramtypes", [user_service_1.UserService, otp_service_1.OtpService, event_emitter_1.EventEmitter2]) ], SendSignUpOtpPhoneController); exports.SendSignUpOtpPhoneController = SendSignUpOtpPhoneController; //# sourceMappingURL=send-sign-up-otp-phone.controller.js.map