UNPKG

@alba-cars/common-modules

Version:

A package containing DTOs, validation classes and common modules and interfaces for Alba Cars

350 lines (349 loc) 13.4 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FaqResponseDTO = exports.FaqFetchDTO = exports.FaqOptionsDTO = exports.SortDTO = exports.FaqFilterDTO = exports.MultilingualContentFetch = void 0; const class_transformer_1 = require("class-transformer"); const class_validator_1 = require("class-validator"); const enums_1 = require("../enums"); class MultilingualContentFetch { toPlain() { return { en: this.en, ar: this.ar, ru: this.ru, zh: this.zh }; } static fromPlain(plain) { const content = new MultilingualContentFetch(); content.en = plain.en; content.ar = plain.ar; content.ru = plain.ru; content.zh = plain.zh; return content; } } __decorate([ (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], MultilingualContentFetch.prototype, "en", void 0); __decorate([ (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], MultilingualContentFetch.prototype, "ar", void 0); __decorate([ (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], MultilingualContentFetch.prototype, "ru", void 0); __decorate([ (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], MultilingualContentFetch.prototype, "zh", void 0); exports.MultilingualContentFetch = MultilingualContentFetch; class FaqFilterDTO { constructor(init) { Object.assign(this, init); } validate(shouldValidate = true) { const errors = []; if (shouldValidate) { if (this.category && !Object.values(enums_1.FaqCategory).includes(this.category)) { errors.push(`Invalid category: ${this.category}. Valid categories are: ${Object.values(enums_1.FaqCategory).join(', ')}`); } if (this.searchLanguage && !['en', 'ar', 'ru', 'zh'].includes(this.searchLanguage)) { errors.push(`Invalid search language: ${this.searchLanguage}. Valid languages are: en, ar, ru, zh`); } } return errors; } static fromPlain(plain) { const dto = new FaqFilterDTO(); if (plain) { dto.search = plain.search; dto.searchLanguage = plain.searchLanguage; dto.searchInAnswers = plain.searchInAnswers; // Handle category if (plain.category) { const categoryValue = plain.category.value || plain.category; if (Object.values(enums_1.FaqCategory).includes(categoryValue)) { dto.category = categoryValue; } } // Handle boolean conversions if (typeof plain.isActive === 'string') { dto.isActive = plain.isActive.toLowerCase() === 'true'; } else { dto.isActive = plain.isActive; } if (typeof plain.searchInAnswers === 'string') { dto.searchInAnswers = plain.searchInAnswers.toLowerCase() === 'true'; } else { dto.searchInAnswers = plain.searchInAnswers; } } return dto; } static toPlain(instance) { return (0, class_transformer_1.instanceToPlain)(instance); } } __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], FaqFilterDTO.prototype, "search", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsString)(), __metadata("design:type", String) ], FaqFilterDTO.prototype, "searchLanguage", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsBoolean)(), __metadata("design:type", Boolean) ], FaqFilterDTO.prototype, "searchInAnswers", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsEnum)(enums_1.FaqCategory, { message: 'Invalid category provided' }), __metadata("design:type", String) ], FaqFilterDTO.prototype, "category", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsBoolean)(), __metadata("design:type", Boolean) ], FaqFilterDTO.prototype, "isActive", void 0); exports.FaqFilterDTO = FaqFilterDTO; class SortDTO { } __decorate([ (0, class_validator_1.IsString)(), __metadata("design:type", String) ], SortDTO.prototype, "field", void 0); __decorate([ (0, class_validator_1.IsString)(), __metadata("design:type", String) ], SortDTO.prototype, "direction", void 0); exports.SortDTO = SortDTO; class FaqOptionsDTO { constructor(init) { this.page = 1; this.limit = 10; Object.assign(this, init); } validate(shouldValidate = true) { const errors = []; if (shouldValidate) { // Validate page if (this.page < 1) { errors.push('Page must be greater than 0'); } // Validate limit if (this.limit < 1) { errors.push('Limit must be greater than 0'); } if (this.limit > 100) { errors.push('Limit cannot exceed 100'); } // Validate sort if present if (this.sort) { if (!this.sort.field) { errors.push('Sort field is required when sort is provided'); } if (this.sort.direction && !['asc', 'desc'].includes(this.sort.direction.toLowerCase())) { errors.push('Sort direction must be either "asc" or "desc"'); } } } return errors; } static toPlain(instance) { return (0, class_transformer_1.instanceToPlain)(instance); } static fromPlain(plain) { var _a; const dto = new FaqOptionsDTO(); if (plain) { // Convert and validate page dto.page = plain.page ? Number(plain.page) : 1; // Convert and validate limit dto.limit = plain.limit ? Number(plain.limit) : 10; // Handle sort if present if (plain.sort) { dto.sort = { field: plain.sort.field, direction: (_a = plain.sort.direction) === null || _a === void 0 ? void 0 : _a.toLowerCase() }; } } return dto; } } __decorate([ (0, class_validator_1.IsNumber)(), (0, class_validator_1.Min)(1), __metadata("design:type", Number) ], FaqOptionsDTO.prototype, "page", void 0); __decorate([ (0, class_transformer_1.Type)(() => SortDTO), (0, class_validator_1.IsNumber)(), (0, class_validator_1.Min)(1), (0, class_validator_1.Max)(100) // Optional: add max limit to prevent excessive requests , __metadata("design:type", Number) ], FaqOptionsDTO.prototype, "limit", void 0); __decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.ValidateNested)(), (0, class_transformer_1.Type)(() => SortDTO), __metadata("design:type", Object) ], FaqOptionsDTO.prototype, "sort", void 0); exports.FaqOptionsDTO = FaqOptionsDTO; class FaqFetchDTO { static fromPlain(plain) { return (0, class_transformer_1.plainToClass)(FaqFetchDTO, { filter: plain.filter, options: plain.options }); } constructor(init) { Object.assign(this, init); } static toPlain(instance) { return (0, class_transformer_1.instanceToPlain)(instance); } } __decorate([ (0, class_transformer_1.Type)(() => FaqFilterDTO), (0, class_validator_1.ValidateNested)(), (0, class_validator_1.IsOptional)(), __metadata("design:type", FaqFilterDTO) ], FaqFetchDTO.prototype, "filter", void 0); __decorate([ (0, class_transformer_1.Type)(() => FaqOptionsDTO), (0, class_validator_1.ValidateNested)(), (0, class_validator_1.IsOptional)(), __metadata("design:type", FaqOptionsDTO) ], FaqFetchDTO.prototype, "options", void 0); exports.FaqFetchDTO = FaqFetchDTO; class FaqResponseDTO { toPlain() { return { id: this.id, question: this.question.toPlain(), answer: this.answer.toPlain(), category: this.category, isActive: this.isActive, order: this.order, createdAt: this.createdAt, updatedAt: this.updatedAt }; } static fromPlain(plain) { if (!plain) { throw new Error('Cannot create FaqResponseDTO from null or undefined'); } const dto = new FaqResponseDTO(); try { dto.id = plain.id; dto.question = MultilingualContentFetch.fromPlain(plain.question); dto.answer = MultilingualContentFetch.fromPlain(plain.answer); dto.category = plain.category; dto.isActive = Boolean(plain.isActive); dto.order = Number(plain.order); dto.createdAt = plain.createdAt; dto.updatedAt = plain.updatedAt; return dto; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to create FaqResponseDTO: ${error.message}`); } else { throw new Error('Failed to create FaqResponseDTO: Unknown error'); } } } // Optional: Add a validate method if needed validate() { const errors = []; if (!this.id) errors.push('ID is required'); if (!this.question) errors.push('Question is required'); if (!this.answer) errors.push('Answer is required'); if (!this.category) errors.push('Category is required'); if (this.isActive === undefined) errors.push('isActive is required'); if (this.order === undefined) errors.push('Order is required'); if (!this.createdAt) errors.push('CreatedAt is required'); if (!this.updatedAt) errors.push('UpdatedAt is required'); // Add any additional validation logic here return errors; } } __decorate([ (0, class_validator_1.IsString)(), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", String) ], FaqResponseDTO.prototype, "id", void 0); __decorate([ (0, class_validator_1.ValidateNested)(), (0, class_transformer_1.Type)(() => MultilingualContentFetch), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", MultilingualContentFetch) ], FaqResponseDTO.prototype, "question", void 0); __decorate([ (0, class_validator_1.ValidateNested)(), (0, class_transformer_1.Type)(() => MultilingualContentFetch), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", MultilingualContentFetch) ], FaqResponseDTO.prototype, "answer", void 0); __decorate([ (0, class_validator_1.IsString)(), (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.IsEnum)(enums_1.FaqCategory) // Add enum validation if you have FaqCategory enum , __metadata("design:type", String) ], FaqResponseDTO.prototype, "category", void 0); __decorate([ (0, class_validator_1.IsBoolean)(), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", Boolean) ], FaqResponseDTO.prototype, "isActive", void 0); __decorate([ (0, class_validator_1.IsNumber)(), (0, class_validator_1.IsNotEmpty)(), (0, class_validator_1.Min)(0) // Add minimum value validation , __metadata("design:type", Number) ], FaqResponseDTO.prototype, "order", void 0); __decorate([ (0, class_validator_1.IsString)(), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", String) ], FaqResponseDTO.prototype, "createdAt", void 0); __decorate([ (0, class_validator_1.IsString)(), (0, class_validator_1.IsNotEmpty)(), __metadata("design:type", String) ], FaqResponseDTO.prototype, "updatedAt", void 0); exports.FaqResponseDTO = FaqResponseDTO;