Skip to content

Instantly share code, notes, and snippets.

@MaxLarue
Last active July 9, 2022 14:51
Show Gist options
  • Save MaxLarue/07c87b7e5439fcdcbe7732baa18bb94f to your computer and use it in GitHub Desktop.
Save MaxLarue/07c87b7e5439fcdcbe7732baa18bb94f to your computer and use it in GitHub Desktop.
@Injectable()
export class AppointmentService {
constructor(@InjectRepository(Appointment) private readonly repository: Repository<Appointment>,
@Inject(QuotationService) private readonly quotationService: QuotationService,
@Inject(WorkOrderService) private readonly workOrderService: WorkOrderService,
@Inject(EmailService) private readonly emailService: EmailService,
@Inject(StatusChangeHistoryService) private readonly statusChangeHistoryService: StatusChangeHistoryService,
@Inject(AppointmentPermissionService) private readonly appointmentPermissionService: AppointmentPermissionService) {}
async create(values: DeepPartial<Appointment>, currentUser: User) {
await this.appointmentPermissionService.verifyCanCreate(currentUser, values)
const appointment = await this.repository.save(values)
for (const quotations of appointment.workOrder.quotations) {
if (quotation.id === appointment.quotationId) {
await this.emailService.sendQuotationWonEmail(quotation.serviceProvider.email, quotation)
await this.quotationService.markQuotationAsWon(quotation)
await this.statusChangeHistoryService.quotationWon(quotation)
} else {
await this.emailService.sendQuotationLostEmail(quotation.serviceProvider.email, quotation)
await this.quotationService.markQuotationAsLost(quotation)
await this.statusChangeHistoryService.quotationLost(quotation)
}
}
await this.workOrderService.markAppointmentCreated(appointment.workOrder)
await this.statusChangeHistoryService.appointmentCreated(appointment)
return appointment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment