import { HttpStatus } from '@nestjs/common'; import { Type } from 'class-transformer'; import { IsArray, IsEnum, IsInt, IsNotEmpty, IsNumber, IsOptional, IsString, Max, Min, ValidateNested } from 'class-validator'; export enum SortDirection { asc = 1, desc = -1, } export class RESTSort { @IsNotEmpty() @IsString() property: string; @IsNotEmpty() @IsEnum(SortDirection) direction: SortDirection; } export interface IMongoPage { skip: number; limit: number; } export class RESTPaging { @IsNotEmpty() @IsInt() @Type(() => Number) @Min(1) page: number; @IsNotEmpty() @IsInt() @Type(() => Number) @Min(1) @Max(100) size: number; public extract() { const skip = (this.page - 1) * this.size; return { skip, limit: this.size }; } public static generate(page: number, size: number): IMongoPage { const skip = (page - 1) * size; return { skip, limit: size }; } } export abstract class RESTRequestBody { @IsNotEmpty() @ValidateNested() abstract data: any; @IsOptional() @ValidateNested() @Type(() => RESTPaging) paging: RESTPaging; @IsOptional() @IsArray() @ValidateNested() @Type(() => RESTSort) sort: RESTSort[]; public isPagingPresent() { if (this.paging && this.paging.page) { return true; } else { return false; } } public isSortPresent() { if (this.sort && this.sort.length) { return true; } else { return false; } } public extractPaging() { if (this.isPagingPresent()) { return this.paging.extract(); } return null; } public extractSort() { const sort = {}; if (this.isSortPresent()) { for (let item of this.sort) { sort[item.property] = item.direction; } } return sort; } } export class RESTPage { // გვერდი page: number; // ჩანაწერების რაოდენობა გვერდზე size: number; // გვერდების ჯამური რაოდენობა number: number; // ჩანაწერების ჯამური რაოდენობა total: number; public static create(count: number, paging: RESTPaging): RESTPage { const page = new RESTPage(); page.page = paging.page; page.size = paging.size; page.number = Math.ceil(count / paging.size); page.total = count; return page; } } export class RESTResult { data: T; page: RESTPage; sort: RESTSort[]; } export class RESTError { keyword: string; debug?: any; validation?: any; statusCode?: HttpStatus; constructor(keyword: string, debug?: any, statusCode?: HttpStatus) { this.keyword = keyword; this.debug = debug; this.statusCode = statusCode || HttpStatus.BAD_REQUEST; } /** * @deprecated The method should not be used.. use new RESTError('error_keyword', 'debug message', HttpStatus.BAD_REQUEST); */ public static throw(keyword: string, statusCode?: HttpStatus, debug?: any) { const error = new RESTError(keyword, debug); error.statusCode = statusCode || HttpStatus.BAD_REQUEST; throw error; } public static dataValidation(validation: any) { const error = new RESTError('body_validation_error', null, 422); error.validation = validation; return error; } } export class RESTResponseBody { public statusCode: number; public result: RESTResult; public errors: RESTError[]; public addError(error: RESTError): void { if (!this.errors) { this.errors = []; } this.errors.push(error); } public static success(data: T, page?: RESTPage, sort?: RESTSort[]): RESTResponseBody { const response = new RESTResponseBody(); response.statusCode = 200; response.result = new RESTResult(); response.result.data = data; response.result.page = page; response.result.sort = sort; return response; } public static fail(errors: RESTError[], statusCode?: number): RESTResponseBody { const response = new RESTResponseBody(); response.statusCode = statusCode; response.errors = errors; return response; } } export class RESTQueryParams extends RESTPaging { @IsOptional() @IsString({ each: true }) sort: string[]; public getRESTSort(): RESTSort[] { const result = []; const items = []; if (this.sort) { if (this.sort instanceof Array) { items.push(...this.sort); } else { items.push(this.sort); } } for (let item of items) { const pair = item.split(':'); if (pair.length != 2) { throw new RESTError('invalid_sorting_pair', pair, 422); } if (['asc', 'desc'].indexOf(pair[1]) === -1) { throw new RESTError('invalid_sorting_direction', pair[1], 422); } result.push({ property: pair[0], direction: pair[1] === 'asc' ? 1 : -1, }); } return result; } public extractSort() { const sorts = this.getRESTSort(); const result = {}; for (let item of sorts) { result[item.property] = item.direction; } return result; } }