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 | import { Injectable, Logger, NotFoundException } from '@nestjs/common'; import { EventBusService } from '../../../platform/events/event-bus.service'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Timesheet } from '../schemas/timesheet.schema'; import { TimeEntry } from '../schemas/time-entry.schema'; @Injectable() export class TimesheetService { private readonly logger = new Logger(TimesheetService.name); constructor( @InjectModel(Timesheet.name) private readonly timesheetModel: Model<Timesheet>, @InjectModel(TimeEntry.name) private readonly timeEntryModel: Model<TimeEntry>, private readonly eventBus: EventBusService, ) {} async generateTimesheet( tenantId: string, employeeId: string, startDate: Date, endDate: Date, ): Promise<any> { const entries = await this.timeEntryModel .find({ tenantId, employeeId, date: { $gte: startDate, $lte: endDate }, stopwatchStatus: 'completed', }) .exec(); const totalHours = entries.reduce((acc, val) => acc + val.hours, 0); const billableHours = entries .filter((e) => e.billable) .reduce((acc, val) => acc + val.hours, 0); const sheet = new this.timesheetModel({ tenantId, employeeId, startDate, endDate, totalHours, billableHours, status: 'draft', }); return sheet.save(); } async submit(tenantId: string, id: string): Promise<any> { const timesheet = await this.timesheetModel .findOneAndUpdate( { _id: id, tenantId }, { status: 'submitted' }, { new: true }, ) .exec(); if (timesheet) { await this.eventBus.publish( 'timesheet.submitted.v1', { timesheetId: timesheet._id, employeeId: timesheet.employeeId }, tenantId, ); } return timesheet; } async approve( tenantId: string, id: string, approverId: string, ): Promise<any> { // Reconciliation against attendance would happen here in a real scenario return this.timesheetModel .findOneAndUpdate( { _id: id, tenantId }, { status: 'approved', approverId, approvedAt: new Date() }, { new: true }, ) .exec(); } } |