Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import { Injectable, NotFoundException, ConflictException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { HolidayCalendar, Holiday, HolidayCalendarAssignment, } from '../schemas'; import { CreateHolidayCalendarDto, UpdateHolidayCalendarDto, CreateHolidayDto, AssignHolidayCalendarDto, } from './dto/holiday.dto'; @Injectable() export class HolidayService { constructor( @InjectModel(HolidayCalendar.name) private calendarModel: Model<HolidayCalendar>, @InjectModel(Holiday.name) private holidayModel: Model<Holiday>, @InjectModel(HolidayCalendarAssignment.name) private assignmentModel: Model<HolidayCalendarAssignment>, ) {} async createCalendar( tenantId: string, createDto: CreateHolidayCalendarDto, userId: string, ) { const existing = await this.calendarModel.findOne({ tenantId, calendarCode: createDto.calendarCode, year: createDto.year, }); if (existing) { throw new ConflictException( `Holiday calendar with code ${createDto.calendarCode} for year ${createDto.year} already exists`, ); } const created = new this.calendarModel({ ...createDto, tenantId, createdBy: userId, }); return created.save(); } async findAllCalendars(tenantId: string, year?: number) { const query: any = { tenantId }; if (year) query.year = year; return this.calendarModel.find(query).exec(); } async findCalendar(tenantId: string, id: string) { const calendar = await this.calendarModel .findOne({ _id: id, tenantId }) .exec(); if (!calendar) { throw new NotFoundException(`Holiday Calendar #${id} not found`); } return calendar; } async updateCalendar( tenantId: string, id: string, updateDto: UpdateHolidayCalendarDto, ) { const updated = await this.calendarModel.findOneAndUpdate( { _id: id, tenantId }, { $set: updateDto }, { new: true }, ); if (!updated) { throw new NotFoundException(`Holiday Calendar #${id} not found`); } return updated; } async addHolidays( tenantId: string, calendarId: string, holidaysDto: CreateHolidayDto[], ) { await this.findCalendar(tenantId, calendarId); const holidays = holidaysDto.map((holiday) => ({ ...holiday, tenantId, holidayCalendarId: calendarId, })); return this.holidayModel.insertMany(holidays); } async getHolidays(tenantId: string, calendarId: string) { return this.holidayModel .find({ tenantId, holidayCalendarId: calendarId }) .sort({ date: 1 }) .exec(); } async assignCalendar( tenantId: string, calendarId: string, assignDto: AssignHolidayCalendarDto, ) { await this.findCalendar(tenantId, calendarId); const assignment = new this.assignmentModel({ ...assignDto, tenantId, holidayCalendarId: calendarId, }); return assignment.save(); } } |