import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { TaskDataEntity } from '../entity/task-data.entity'; import { EntityManager, Repository } from 'typeorm'; import { EntityDynamicService } from 'src/module/meta/service/entity-dynamic.service'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; import { UserData } from 'src/module/user/entity/user.entity'; import { ConfigService } from '@nestjs/config'; @Injectable() export class TaskRepository { constructor( @InjectRepository(TaskDataEntity) private readonly taskRepository: Repository, private readonly entityDynamicService: EntityDynamicService, private readonly reflectionHelper: ReflectionHelper, private readonly entityManager: EntityManager, private readonly configService: ConfigService, ) {} schema = this.configService.get('DB_SCHEMA'); async getAllTaskByUserIdAndStageId(condition) { const result = await this.taskRepository.find({ where: { ...condition, }, }); return result; } async saveActionDataInTask( action: any, loggedInUser, mapped_entity_id: number, mapped_entity_type: string, ): Promise { if (!action?.length) return; const listMasterItemsRepo = this.reflectionHelper.getRepoService('ListMasterItems'); const listMasterItem = await listMasterItemsRepo.findOne({ where: { code: 'in_progress', enterprise_id: loggedInUser.enterprise_id, listtype: 'TKST', }, }); const leadData = await this.entityManager.query( `SELECT * FROM ${this.schema}.crm_lead WHERE id = $1`, [mapped_entity_id], ); // Find the action with the lowest sequence if (action.length > 0) { for (const act of action) { // search that is_mandatory is true or not in list_master_items for the given organization const mandatoryListMasterItem = await listMasterItemsRepo.find({ where: { id: act.action_requirement, }, }); const now = new Date(); const dueDateTime = new Date(now); dueDateTime.setDate(dueDateTime.getDate() + 2); // Force conversion to IST const istFormatterDate = new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Kolkata', }); const istFormatterTime = new Intl.DateTimeFormat('en-US', { timeZone: 'Asia/Kolkata', hour: '2-digit', minute: '2-digit', hour12: true, }); const due_date = istFormatterDate.format(dueDateTime); // YYYY-MM-DD in IST const due_time = istFormatterTime.format(dueDateTime); // e.g. "03:14 AM" console.log({ due_date, due_time }); const taskCode = await this.entityDynamicService.getCode( 'TASK', loggedInUser, ); const taskData = this.taskRepository.create({ stage_id: act.stage_id, user_id: loggedInUser.id, action_id: act.id, organization_id: loggedInUser.organization_id, level_type: loggedInUser.level_type, level_id: loggedInUser.level_id, enterprise_id: loggedInUser.enterprise_id, name: act.name, sequence: act.sequence, is_mandatory: mandatoryListMasterItem[0]?.code === 'mandatory' ? true : false, mapped_entity_id, mapped_entity_type, due_date, due_time, is_system: true, status: listMasterItem?.id, category: act?.action_category_code, task_owner: leadData[0]?.lead_owner, code: taskCode, }); await this.taskRepository.save(taskData); } } } // update task status async updateTaskStatus( loggedInUser: UserData, mapped_entity_type: string, mapped_entity_id: number, stage_id: any, action_id: any, ): Promise { const task = await this.taskRepository.findOneBy({ action_id, mapped_entity_type, mapped_entity_id, stage_id, }); if (!task) return; const listMasterItemsRepo = this.reflectionHelper.getRepoService('ListMasterItems'); const listMasterItem = await listMasterItemsRepo.findOne({ where: { code: 'completed', enterprise_id: loggedInUser.enterprise_id, listtype: 'TKST', }, }); task.modified_by = loggedInUser.id; task.modified_date = new Date(); task.is_done = true; task.status = listMasterItem?.id; await this.taskRepository.update(task.id, task); } }