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 | import { Injectable, NotFoundException, ConflictException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Shift, ShiftVersion, ShiftBreakRule } from '../schemas'; import { CreateShiftDto, UpdateShiftDto, CreateShiftBreakRuleDto, } from './dto/shift.dto'; @Injectable() export class ShiftService { constructor( @InjectModel(Shift.name) private shiftModel: Model<Shift>, @InjectModel(ShiftVersion.name) private versionModel: Model<ShiftVersion>, @InjectModel(ShiftBreakRule.name) private breakRuleModel: Model<ShiftBreakRule>, ) {} async create(tenantId: string, createDto: CreateShiftDto, userId: string) { const existing = await this.shiftModel.findOne({ tenantId, shiftCode: createDto.shiftCode, }); if (existing) { throw new ConflictException( `Shift with code ${createDto.shiftCode} already exists`, ); } const created = new this.shiftModel({ ...createDto, tenantId, createdBy: userId, }); const saved = await created.save(); await this.versionModel.create({ tenantId, shiftId: saved._id, shiftData: saved.toObject(), effectiveFrom: saved.effectiveFrom, createdBy: userId, }); return saved; } async findAll(tenantId: string) { return this.shiftModel.find({ tenantId }).exec(); } async findOne(tenantId: string, id: string) { const shift = await this.shiftModel.findOne({ _id: id, tenantId }).exec(); if (!shift) { throw new NotFoundException(`Shift #${id} not found`); } return shift; } async update( tenantId: string, id: string, updateDto: UpdateShiftDto, userId: string, ) { const shift = await this.findOne(tenantId, id); const updated = await this.shiftModel.findOneAndUpdate( { _id: id, tenantId }, { $set: updateDto }, { new: true }, ); if (!updated) { throw new NotFoundException(`Shift #${id} not found`); } await this.versionModel.create({ tenantId, shiftId: updated._id, shiftData: updated.toObject(), effectiveFrom: updated.effectiveFrom, createdBy: userId, }); return updated; } async addBreakRule( tenantId: string, shiftId: string, breakRuleDto: CreateShiftBreakRuleDto, ) { await this.findOne(tenantId, shiftId); const created = new this.breakRuleModel({ ...breakRuleDto, tenantId, shiftId, }); return created.save(); } async getBreakRules(tenantId: string, shiftId: string) { return this.breakRuleModel.find({ tenantId, shiftId }).exec(); } } |