@bitwild/rockets
Version:
Rockets - Core server functionality for NestJS applications with built-in authentication, user management, and API scaffolding
110 lines • 5.43 kB
JavaScript
;
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); }
};
var GenericUserMetadataModelService_1;
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 error_logging_helper_1 = require("../../../utils/error-logging.helper");
const user_metadata_constants_1 = require("../constants/user-metadata.constants");
let GenericUserMetadataModelService = GenericUserMetadataModelService_1 = class GenericUserMetadataModelService extends nestjs_common_1.ModelService {
constructor(repo, createDto, updateDto) {
super(repo);
this.repo = repo;
this.logger = new common_1.Logger(GenericUserMetadataModelService_1.name);
this.createDto = createDto;
this.updateDto = updateDto;
}
async getUserMetadataById(id) {
try {
const userMetadata = await this.byId(id);
if (!userMetadata) {
throw new common_1.NotFoundException(`UserMetadata with ID ${id} not found`);
}
return userMetadata;
}
catch (error) {
if (error instanceof nestjs_common_1.RuntimeException) {
throw error;
}
(0, error_logging_helper_1.logAndGetErrorDetails)(error, this.logger, 'Failed to fetch user metadata', { id, errorId: 'USER_METADATA_FETCH_FAILED' });
throw new common_1.InternalServerErrorException('Failed to fetch user metadata');
}
}
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({ id: existingUserMetadata.id }, data);
return this.update(updateData);
}
else {
const createData = Object.assign({ userId }, data);
return this.create(createData);
}
}
async getUserMetadataByUserId(userId) {
try {
const userMetadata = await this.findByUserId(userId);
if (!userMetadata) {
throw new common_1.NotFoundException(`UserMetadata for user ID ${userId} not found`);
}
return userMetadata;
}
catch (error) {
if (error instanceof nestjs_common_1.RuntimeException) {
throw error;
}
(0, error_logging_helper_1.logAndGetErrorDetails)(error, this.logger, 'Failed to fetch user metadata', { userId, errorId: 'USER_METADATA_FETCH_BY_USER_FAILED' });
throw new common_1.InternalServerErrorException('Failed to fetch user metadata');
}
}
async update(data) {
const { id } = data;
if (!id) {
throw new common_1.BadRequestException('ID is required for update operation');
}
try {
const existing = await this.repo.findOne({ where: { id } });
if (!existing) {
throw new common_1.NotFoundException(`UserMetadata with ID ${id} not found`);
}
return super.update(data);
}
catch (error) {
if (error instanceof nestjs_common_1.RuntimeException) {
throw error;
}
(0, error_logging_helper_1.logAndGetErrorDetails)(error, this.logger, 'Failed to update user metadata', { id, errorId: 'USER_METADATA_UPDATE_FAILED' });
throw new common_1.InternalServerErrorException('Failed to update user metadata');
}
}
};
exports.GenericUserMetadataModelService = GenericUserMetadataModelService;
exports.GenericUserMetadataModelService = GenericUserMetadataModelService = GenericUserMetadataModelService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, nestjs_common_1.InjectDynamicRepository)(user_metadata_constants_1.USER_METADATA_MODULE_ENTITY_KEY)),
__metadata("design:paramtypes", [Object, Function, Function])
], GenericUserMetadataModelService);
//# sourceMappingURL=user-metadata.model.service.js.map