UNPKG

@bitwild/rockets-auth

Version:

Rockets Auth - Complete authentication and authorization solution for NestJS with JWT, OAuth, OTP, role-based access control, and more

84 lines 3.94 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.GenericUserMetadataModelService = void 0; const common_1 = require("@nestjs/common"); const nestjs_common_1 = require("@concepta/nestjs-common"); const user_metadata_constants_1 = require("../constants/user-metadata.constants"); const user_metadata_exception_1 = require("../user-metadata.exception"); let GenericUserMetadataModelService = class GenericUserMetadataModelService extends nestjs_common_1.ModelService { constructor(repo, createDto, updateDto) { super(repo); this.repo = repo; this.createDto = createDto; this.updateDto = updateDto; } async validate(_type, data) { return Promise.resolve(data); } async getUserMetadataById(id) { const userMetadata = await this.byId(id); if (!userMetadata) { throw new user_metadata_exception_1.UserMetadataNotFoundException(); } return userMetadata; } async updateUserMetadata(userId, userMetadataData) { const userMetadata = await this.getUserMetadataByUserId(userId); return this.update(Object.assign(Object.assign({}, userMetadata), userMetadataData)); } async findByUserId(userId) { return this.repo.findOne({ where: { userId } }); } async hasUserMetadata(userId) { const userMetadata = await this.findByUserId(userId); return !!userMetadata; } async createOrUpdate(userId, data) { const existingUserMetadata = await this.findByUserId(userId); if (existingUserMetadata) { const updateData = Object.assign(Object.assign({}, existingUserMetadata), data); return this.update(updateData); } else { const createData = Object.assign({ userId }, data); return this.create(createData); } } async getUserMetadataByUserId(userId) { const userMetadata = await this.findByUserId(userId); if (!userMetadata) { throw new user_metadata_exception_1.UserMetadataNotFoundException(); } return userMetadata; } async update(data) { const { id } = data; if (!id) { throw new user_metadata_exception_1.UserMetadataException('ID is required for update operation'); } const existing = await this.repo.findOne({ where: { id } }); if (!existing) { throw new user_metadata_exception_1.UserMetadataNotFoundException(); } return super.update(data); } }; exports.GenericUserMetadataModelService = GenericUserMetadataModelService; exports.GenericUserMetadataModelService = GenericUserMetadataModelService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(user_metadata_constants_1.AUTH_USER_METADATA_MODULE_ENTITY_KEY)), __metadata("design:paramtypes", [Object, Function, Function]) ], GenericUserMetadataModelService); //# sourceMappingURL=rockets-auth-user-metadata.model.service.js.map