From 6fbccb0c9dba0033c2d03f92912f73d3a0ebb01b Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:55:14 +0700 Subject: [PATCH] feat: integration feature report bookmark --- .../report-bookmark.controller.ts | 5 + .../report-bookmark.service.ts | 121 ++++++++++++++++-- .../report-export/report-export.service.ts | 4 +- src/modules/reports/report/report.service.ts | 4 +- .../shared/dto/report-bookmark.get.dto.ts | 4 +- .../shared/services/base-report.service.ts | 26 +++- 6 files changed, 147 insertions(+), 17 deletions(-) diff --git a/src/modules/reports/report-bookmark/report-bookmark.controller.ts b/src/modules/reports/report-bookmark/report-bookmark.controller.ts index 08df759..dfb067b 100644 --- a/src/modules/reports/report-bookmark/report-bookmark.controller.ts +++ b/src/modules/reports/report-bookmark/report-bookmark.controller.ts @@ -35,6 +35,11 @@ export class ReportBookmarkController { return await this.service.getAll(query); } + @Get('id') + async get(@Param('id') id: string) { + return await this.service.getOne(id); + } + @Get('label-history') async getAllLabelHistory(@Query() query: GetLabelReportBookmarkDto) { return await this.service.getAllLabelHistory(query); diff --git a/src/modules/reports/report-bookmark/report-bookmark.service.ts b/src/modules/reports/report-bookmark/report-bookmark.service.ts index 7cbe05f..ccf8cb6 100644 --- a/src/modules/reports/report-bookmark/report-bookmark.service.ts +++ b/src/modules/reports/report-bookmark/report-bookmark.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable, Scope } from '@nestjs/common'; import { BaseReportService } from '../shared/services/base-report.service'; import { CreateReportBookmarkDto } from '../shared/dto/report-bookmark.create.dto'; import { @@ -9,9 +9,13 @@ import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { ReportBookmarkModel } from '../shared/models/report-bookmark.model'; import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants'; +import { UserProvider } from 'src/core/sessions'; -@Injectable() +@Injectable({ scope: Scope.REQUEST }) export class ReportBookmarkService extends BaseReportService { + @Inject() + protected userProvider: UserProvider; + constructor( @InjectRepository(ReportBookmarkModel, CONNECTION_NAME.DEFAULT) private repo: Repository, @@ -20,15 +24,17 @@ export class ReportBookmarkService extends BaseReportService { } async create(body: CreateReportBookmarkDto) { - return 'you hit API for create report bookmark'; + const newPayload = this.injectDefaultColumnCreate(body); + return await this.repo.save(newPayload); } async getAll(query: GetReportBookmarkDto) { const modelName = ReportBookmarkModel.name; - const requestor_id = this.userProvider.user.id; + const creator_id = this.getUser().id; const unique_names = query.unique_names; const group_names = query.group_names; + const types = query.types; const qb = this.repo .createQueryBuilder(modelName) @@ -37,28 +43,123 @@ export class ReportBookmarkService extends BaseReportService { query.andWhere(`unique_name IN (:...unique_names)`, { unique_names }); } if (group_names) { - query.andWhere(`group_name =IN (:...group_names)`, { group_names }); + query.andWhere(`group_name IN (:...group_names)`, { group_names }); } - query.andWhere(`requestor_id = :requestor_id`, { requestor_id }); + + if (types) { + query.andWhere(`type IN (:...types)`, { types }); + } + + query.andWhere(`creator_id = :creator_id`, { creator_id }); }) .orderBy(`${modelName}.created_at`, 'DESC'); return await qb.getMany(); } + async getOne(id: string) { + return await this.repo.findOneBy({ id }); + } + async getAllLabelHistory(query: GetLabelReportBookmarkDto) { - return 'you hit API for get all label history report bookmark'; + const modelName = ReportBookmarkModel.name; + + const creator_id = this.getUser().id; + const unique_names = query.unique_names; + const group_names = query.group_names; + const types = query.types; + + const qb = this.repo + .createQueryBuilder(modelName) + .select(`${modelName}.label`) + .where((query) => { + if (unique_names) { + query.andWhere(`unique_name IN (:...unique_names)`, { unique_names }); + } + if (group_names) { + query.andWhere(`group_name IN (:...group_names)`, { group_names }); + } + + if (types) { + query.andWhere(`type IN (:...types)`, { types }); + } + + query.andWhere(`creator_id = :creator_id`, { creator_id }); + }) + .distinct(true); + + const newData = await qb.getRawMany(); + return newData.map((el) => el.ReportBookmarkModel_label); } async applied(id: string) { - return 'you hit API for applied report bookmark'; + await this.repo + .createQueryBuilder() + .update(ReportBookmarkModel) + .set({ applied: true }) + .where((query) => { + query.andWhere(`id = :id`, { id }); + }) + .execute(); + + const data = await this.appliedFalseBookmark(id); + const group_name = data.group_name; + const unique_name = data.unique_name; + const type = data.type; + + return await this.getAll({ + group_names: [group_name], + unique_names: [unique_name], + types: [type], + }); } async unapplied(id: string) { - return 'you hit API for unapplied report bookmark'; + await this.repo + .createQueryBuilder() + .update(ReportBookmarkModel) + .set({ applied: false }) + .where((query) => { + query.andWhere(`id = :id`, { id }); + }) + .execute(); + + const data = await this.getOne(id); + const group_name = data.group_name; + const unique_name = data.unique_name; + const type = data.type; + + return await this.getAll({ + group_names: [group_name], + unique_names: [unique_name], + types: [type], + }); } async delete(id: string) { - return 'you hit API for delete report bookmark'; + await this.repo.delete(id); + return { + success: true, + message: `Successfully deleted bookmark with id "${id}"`, + }; + } + + async appliedFalseBookmark(id: string) { + const data = await this.getOne(id); + const creator_id = data.creator_id; + const type = data.type; + + await this.repo + .createQueryBuilder() + .update(ReportBookmarkModel) + .set({ applied: false }) + .where((query) => { + query.andWhere(`id != :id`, { id }); + query.andWhere(`creator_id = :creator_id`, { creator_id }); + query.andWhere(`type = :type`, { type }); + }) + .execute(); + + return data; } } diff --git a/src/modules/reports/report-export/report-export.service.ts b/src/modules/reports/report-export/report-export.service.ts index 1586329..b8096c4 100644 --- a/src/modules/reports/report-export/report-export.service.ts +++ b/src/modules/reports/report-export/report-export.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Scope } from '@nestjs/common'; import { BaseReportService } from '../shared/services/base-report.service'; import { CreateReportExportDto } from '../shared/dto/report-export.create.dto'; import { @@ -11,7 +11,7 @@ import { ExportReportHistoryModel } from '../shared/models/export-report-history import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants'; import { DataSource, Repository } from 'typeorm'; -@Injectable() +@Injectable({ scope: Scope.REQUEST }) export class ReportExportService extends BaseReportService { constructor( @InjectDataSource(CONNECTION_NAME.DEFAULT) diff --git a/src/modules/reports/report/report.service.ts b/src/modules/reports/report/report.service.ts index 9bcb8fe..ff5f488 100644 --- a/src/modules/reports/report/report.service.ts +++ b/src/modules/reports/report/report.service.ts @@ -1,4 +1,4 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { Injectable, Logger, Scope } from '@nestjs/common'; import { BaseReportService } from '../shared/services/base-report.service'; import { GetReportConfigDto } from '../shared/dto/report-config.get.dto'; import { GetReportDataDto } from '../shared/dto/report-data.get.dto'; @@ -11,7 +11,7 @@ import { ReportQueryBuilder } from '../shared/helpers'; import { DATA_FORMAT } from '../shared/constant'; import { roundingCurrency } from '../shared/helpers/rounding-currency'; -@Injectable() +@Injectable({ scope: Scope.REQUEST }) export class ReportService extends BaseReportService { private readonly logger = new Logger(ReportService.name); diff --git a/src/modules/reports/shared/dto/report-bookmark.get.dto.ts b/src/modules/reports/shared/dto/report-bookmark.get.dto.ts index b3d8ba2..655828c 100644 --- a/src/modules/reports/shared/dto/report-bookmark.get.dto.ts +++ b/src/modules/reports/shared/dto/report-bookmark.get.dto.ts @@ -7,7 +7,7 @@ export class GetReportBookmarkDto { @Transform((body) => { return Array.isArray(body.value) ? body.value : [body.value]; }) - group_names?: string; + group_names?: string[]; @ApiProperty({ type: ['string'], required: false }) @Transform((body) => { @@ -27,7 +27,7 @@ export class GetLabelReportBookmarkDto { @Transform((body) => { return Array.isArray(body.value) ? body.value : [body.value]; }) - group_names?: string; + group_names?: string[]; @ApiProperty({ type: ['string'], required: false }) @Transform((body) => { diff --git a/src/modules/reports/shared/services/base-report.service.ts b/src/modules/reports/shared/services/base-report.service.ts index dbaefd8..bed19b0 100644 --- a/src/modules/reports/shared/services/base-report.service.ts +++ b/src/modules/reports/shared/services/base-report.service.ts @@ -3,7 +3,7 @@ import { UserProvider } from 'src/core/sessions'; import { BLANK_USER } from 'src/core/strings/constants/base.constants'; @Injectable() -export class BaseReportService { +export class BaseReportService { @Inject() protected userProvider: UserProvider; @@ -14,4 +14,28 @@ export class BaseReportService { return BLANK_USER; } } + + injectDefaultColumnCreate(payload: PayloadEntity): any { + const currentDate = new Date().getTime(); + const user = this.getUser(); + return { + ...payload, + creator_id: user.id, + creator_name: user.name, + editor_id: user.id, + editor_name: user.name, + created_at: currentDate, + updated_at: currentDate, + }; + } + + injectDefaultColumnUpdate(payload: PayloadEntity) { + const user = this.getUser(); + return { + ...payload, + editor_id: user.id, + editor_name: user.name, + updated_at: new Date().getTime(), + }; + } }