|
| 1 | +import { BadRequestException, Injectable, Logger } from '@nestjs/common'; |
| 2 | + |
| 3 | +import { Mapper } from '@automapper/core'; |
| 4 | +import { InjectMapper } from '@automapper/nestjs'; |
| 5 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 6 | +import { Repository, DataSource } from 'typeorm'; |
| 7 | +import { LogAsyncMethodExecution } from '../../../common/decorator/log-async-method-execution.decorator'; |
| 8 | +import { SuspendActivity } from '../../../common/enum/suspend-activity.enum'; |
| 9 | +import { DataNotFoundException } from '../../../common/exception/data-not-found.exception'; |
| 10 | +import { IUserJWT } from '../../../common/interface/user-jwt.interface'; |
| 11 | +import { Company } from '../company/entities/company.entity'; |
| 12 | +import { IdirUser } from '../users/entities/idir.user.entity'; |
| 13 | +import { CompanyService } from '../company/company.service'; |
| 14 | +import { CreateCompanySuspendDto } from './dto/request/create-company-suspend.dto'; |
| 15 | +import { ReadCompanySuspendActivityDto } from './dto/response/read-company-suspend-activity.dto'; |
| 16 | +import { CompanySuspendActivity } from './entities/company-suspend-activity.entity'; |
| 17 | + |
| 18 | +@Injectable() |
| 19 | +export class CompanySuspendService { |
| 20 | + private readonly logger = new Logger(CompanySuspendService.name); |
| 21 | + constructor( |
| 22 | + @InjectRepository(CompanySuspendActivity) |
| 23 | + private companySuspendRepository: Repository<CompanySuspendActivity>, |
| 24 | + @InjectMapper() private readonly classMapper: Mapper, |
| 25 | + private dataSource: DataSource, |
| 26 | + private readonly companyService: CompanyService, |
| 27 | + ) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * The update() method retrieves the entity from the database using the |
| 31 | + * Repository, maps the DTO object to the entity using the Mapper, sets some |
| 32 | + * additional properties on the entity, and saves it back to the database |
| 33 | + * using the Repository. It then retrieves the updated entity and returns it |
| 34 | + * in a DTO object. |
| 35 | + * |
| 36 | + * |
| 37 | + * @param companyId The company Id. |
| 38 | + * @param updateCompanyDto Request object of type {@link UpdateCompanyDto} for |
| 39 | + * updating a company. |
| 40 | + * @param directory Directory derived from the access token. |
| 41 | + * |
| 42 | + * @returns The company details as a promise of type {@link ReadCompanyDto} |
| 43 | + */ |
| 44 | + @LogAsyncMethodExecution() |
| 45 | + async suspendCompany( |
| 46 | + companyId: number, |
| 47 | + createCompanySuspendDtonyDto: CreateCompanySuspendDto, |
| 48 | + currentUser: IUserJWT, |
| 49 | + ): Promise<ReadCompanySuspendActivityDto> { |
| 50 | + let savedSuspendAcitivity: CompanySuspendActivity; |
| 51 | + const company = await this.companyService.findOne(companyId); |
| 52 | + |
| 53 | + if (!company) { |
| 54 | + throw new DataNotFoundException(); |
| 55 | + } |
| 56 | + const toBeSuspended: boolean = |
| 57 | + createCompanySuspendDtonyDto.suspendActivityType === |
| 58 | + SuspendActivity.SUSPEND_COMPANY; |
| 59 | + if (company.isSuspended === toBeSuspended) { |
| 60 | + throw new BadRequestException( |
| 61 | + `Company ${companyId} is already in ${company.isSuspended ? SuspendActivity.SUSPEND_COMPANY : SuspendActivity.UNSUSPEND_COMPANY} status`, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + const queryRunner = this.dataSource.createQueryRunner(); |
| 66 | + await queryRunner.connect(); |
| 67 | + await queryRunner.startTransaction(); |
| 68 | + try { |
| 69 | + const currentDateTime: Date = new Date(); |
| 70 | + |
| 71 | + await queryRunner.manager |
| 72 | + .createQueryBuilder() |
| 73 | + .update(Company) |
| 74 | + .set({ |
| 75 | + isSuspended: toBeSuspended, |
| 76 | + updatedUser: currentUser.userName, |
| 77 | + updatedDateTime: currentDateTime, |
| 78 | + updatedUserDirectory: currentUser.orbcUserDirectory, |
| 79 | + updatedUserGuid: currentUser.userGUID, |
| 80 | + }) |
| 81 | + .where('companyId = :companyId', { companyId: companyId }) |
| 82 | + .execute(); |
| 83 | + |
| 84 | + const suspendActivity: CompanySuspendActivity = |
| 85 | + new CompanySuspendActivity(); |
| 86 | + |
| 87 | + suspendActivity.companyId = companyId; |
| 88 | + suspendActivity.suspendActivityType = |
| 89 | + createCompanySuspendDtonyDto.suspendActivityType; |
| 90 | + suspendActivity.comment = createCompanySuspendDtonyDto.comment; |
| 91 | + suspendActivity.suspendActivityDateTime = currentDateTime; |
| 92 | + const idirUser = new IdirUser(); |
| 93 | + idirUser.userGUID = currentUser.userGUID; |
| 94 | + suspendActivity.idirUser = idirUser; |
| 95 | + suspendActivity.createdUser = currentUser.userName; |
| 96 | + suspendActivity.createdUserGuid = currentUser.userGUID; |
| 97 | + suspendActivity.createdUserDirectory = currentUser.orbcUserDirectory; |
| 98 | + suspendActivity.createdDateTime = currentDateTime; |
| 99 | + suspendActivity.updatedUser = currentUser.userName; |
| 100 | + suspendActivity.updatedUserGuid = currentUser.userGUID; |
| 101 | + suspendActivity.updatedUserDirectory = currentUser.orbcUserDirectory; |
| 102 | + suspendActivity.updatedDateTime = currentDateTime; |
| 103 | + |
| 104 | + savedSuspendAcitivity = await queryRunner.manager.save(suspendActivity); |
| 105 | + |
| 106 | + await queryRunner.commitTransaction(); |
| 107 | + } catch (error) { |
| 108 | + await queryRunner.rollbackTransaction(); |
| 109 | + this.logger.error(error); |
| 110 | + throw error; |
| 111 | + } finally { |
| 112 | + await queryRunner.release(); |
| 113 | + } |
| 114 | + |
| 115 | + const result = await this.classMapper.mapAsync( |
| 116 | + savedSuspendAcitivity, |
| 117 | + CompanySuspendActivity, |
| 118 | + ReadCompanySuspendActivityDto, |
| 119 | + ); |
| 120 | + result.userName = currentUser.userName?.toUpperCase(); |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * The findAllSuspendActivityByCompanyId() method retrieves all suspend activities associated with a given company ID. |
| 126 | + * It queries the suspend activity repository for records matching the company ID and includes the relation to the IDIR user. |
| 127 | + * The results are then mapped from the CompanySuspendActivity entities to ReadCompanySuspendActivityDto objects for presentation. |
| 128 | + * |
| 129 | + * @param companyId The unique identifier of the company. |
| 130 | + * |
| 131 | + * @returns A promise containing an array of ReadCompanySuspendActivityDto objects representing the suspend activities for the specified company. |
| 132 | + */ |
| 133 | + @LogAsyncMethodExecution() |
| 134 | + async findAllSuspendActivityByCompanyId( |
| 135 | + companyId: number, |
| 136 | + ): Promise<ReadCompanySuspendActivityDto[]> { |
| 137 | + const suspendActivityDbResults = await this.companySuspendRepository.find({ |
| 138 | + where: { companyId: companyId }, |
| 139 | + relations: { |
| 140 | + idirUser: true, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + return await this.classMapper.mapArrayAsync( |
| 145 | + suspendActivityDbResults, |
| 146 | + CompanySuspendActivity, |
| 147 | + ReadCompanySuspendActivityDto, |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments