Advertisement
nodejsdeveloperskh

repository-in-nestjs

Jun 1st, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { MongoError } from 'mongodb';
  2. import { Model } from 'mongoose';
  3. import {
  4.     HttpException,
  5.     HttpStatus,
  6.     Injectable,
  7. } from '@nestjs/common';
  8. import { InjectModel } from '@nestjs/mongoose';
  9.  
  10. import {
  11.     collectionName,
  12.     TicketDepartments,
  13.     TicketDepartmentsDocument,
  14. } from '../schemas/ticket-departments.schema';
  15. import { CreateTicketDepartmentDto } from '../dto/create-ticket-department.dto';
  16. import { UpdateTicketDepartmentDto } from '../dto/update-ticket-department.dto';
  17.  
  18. @Injectable()
  19. class TicketDepartmentsRepository {
  20.     constructor(
  21.         @InjectModel(collectionName)
  22.         private ticketDepartmentModel: Model<TicketDepartmentsDocument>,
  23.     ) {}
  24.  
  25.     async insert(
  26.         createTicketDepartmentDto: CreateTicketDepartmentDto,
  27.     ): Promise<{ _id: string }> {
  28.         const { members, name, title } = createTicketDepartmentDto;
  29.         const ticketDepartment = await new this.ticketDepartmentModel(
  30.             {
  31.                 members,
  32.                 name,
  33.                 title,
  34.             },
  35.         )
  36.             .save()
  37.             .catch(insertErrorCatcher);
  38.  
  39.         return {
  40.             _id: ticketDepartment.id,
  41.         };
  42.     }
  43.  
  44.     async findAll(): Promise<{
  45.         ticketDepartments: TicketDepartments[];
  46.     }> {
  47.         return {
  48.             ticketDepartments: await this.ticketDepartmentModel
  49.                 .find()
  50.                 .exec(),
  51.         };
  52.     }
  53.  
  54.     async findById(_id: string): Promise<{
  55.         ticketDepartment: TicketDepartments;
  56.     }> {
  57.         const ticketDepartment = await this.ticketDepartmentModel
  58.             .findById(_id)
  59.             .exec();
  60.  
  61.         documentExistanceChecker(ticketDepartment);
  62.  
  63.         return {
  64.             ticketDepartment,
  65.         };
  66.     }
  67.  
  68.     async findByIdAndUpdate(
  69.         _id: string,
  70.         updateTicketDepartmentDto: UpdateTicketDepartmentDto,
  71.     ): Promise<{
  72.         ticketDepartment: TicketDepartments;
  73.     }> {
  74.         const ticketDepartment = await this.ticketDepartmentModel
  75.             .findByIdAndUpdate(_id, { ...updateTicketDepartmentDto })
  76.             .exec();
  77.  
  78.         documentExistanceChecker(ticketDepartment);
  79.  
  80.         return {
  81.             ticketDepartment,
  82.         };
  83.     }
  84. }
  85.  
  86. function documentExistanceChecker(document: any) {
  87.     if (!document) {
  88.         throw new HttpException(
  89.             {
  90.                 status: HttpStatus.NOT_FOUND,
  91.                 error: 'Ticket department not found',
  92.             },
  93.             HttpStatus.NOT_FOUND,
  94.         );
  95.     }
  96. }
  97.  
  98. function insertErrorCatcher(error: MongoError) {
  99.     if (error.code == 11000) {
  100.         throw new HttpException(
  101.             {
  102.                 status: HttpStatus.CONFLICT,
  103.                 error: 'Inserted Ticket department confilicts with an existing Ticket department',
  104.             },
  105.             HttpStatus.CONFLICT,
  106.         );
  107.     }
  108. }
  109.  
  110. export { TicketDepartmentsRepository };
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement