feat: integration feature report bookmark

pull/12/head
Firman Ramdhani 2024-07-04 15:55:14 +07:00
parent fc37e0c502
commit 6fbccb0c9d
6 changed files with 147 additions and 17 deletions

View File

@ -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);

View File

@ -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<ReportBookmarkModel>,
@ -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;
}
}

View File

@ -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)

View File

@ -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);

View File

@ -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) => {

View File

@ -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<PayloadEntity = any> {
@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(),
};
}
}