feat(SPG-355) REST API Read Season Period

pull/2/head
ashar 2024-06-20 01:46:09 +07:00
parent 08dea7965d
commit 5b5c3efccc
8 changed files with 252 additions and 53 deletions

View File

@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { SeasonPeriodEntity } from '../../domain/entities/season-period.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { SeasonPeriodModel } from '../models/season-period.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository } from 'typeorm';
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
@Injectable()
export class SeasonPeriodReadService extends BaseReadService<SeasonPeriodEntity> {
constructor(
@InjectRepository(SeasonPeriodModel, CONNECTION_NAME.DEFAULT)
private repo: Repository<SeasonPeriodModel>,
) {
super(repo);
}
}

View File

@ -0,0 +1,9 @@
import { BaseFilterEntity } from 'src/core/modules/domain/entities/base-filter.entity';
import { EnumDays } from '../../constants';
export interface FilterSeasonPeriodEntity extends BaseFilterEntity {
start_date: Date;
end_date: Date;
holiday_names: string[];
days: EnumDays[];
}

View File

@ -0,0 +1,56 @@
import { Injectable } from '@nestjs/common';
import { BaseDetailManager } from 'src/core/modules/domain/usecase/managers/base-detail.manager';
import { SeasonPeriodEntity } from '../../entities/season-period.entity';
import { RelationParam } from 'src/core/modules/domain/entities/base-filter.entity';
@Injectable()
export class DetailSeasonPeriodManager extends BaseDetailManager<SeasonPeriodEntity> {
async prepareData(): Promise<void> {
return;
}
async beforeProcess(): Promise<void> {
return;
}
async afterProcess(): Promise<void> {
return;
}
get relations(): RelationParam {
return {
// relation only join (for query purpose)
joinRelations: ['season_type'],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: [],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [],
};
}
get selects(): string[] {
return [
`${this.tableName}.id`,
`${this.tableName}.created_at`,
`${this.tableName}.creator_name`,
`${this.tableName}.editor_name`,
`${this.tableName}.updated_at`,
`${this.tableName}.status`,
`${this.tableName}.start_date`,
`${this.tableName}.end_date`,
`${this.tableName}.days`,
`${this.tableName}.holiday_name`,
'season_type.id',
'season_type.name',
];
}
get setFindProperties(): any {
return {
id: this.dataId,
};
}
}

View File

@ -1,53 +0,0 @@
import { Injectable } from '@nestjs/common';
import { google } from 'googleapis';
@Injectable()
export class IndexHolidayCalendarManager {
async execute() {
const events = [];
const calendar = google.calendar({
version: 'v3',
auth: 'AIzaSyCsCt6PDd6uYLkahvtdvCoMWf-1_QaLiNM',
});
const calendarId = 'id.indonesian#holiday@group.v.calendar.google.com';
const res = await calendar.events.list({
calendarId: calendarId,
timeMin: new Date().getFullYear() + '-01-01T00:00:00Z',
timeMax: new Date().getFullYear() + '-12-31T23:59:59Z',
singleEvents: true,
orderBy: 'startTime',
});
res.data.items?.forEach((item) => {
// const eventName = item.summary.replace("Joint", "").replace("for", "").replace("after", "").replace("Holiday","").trim();
let eventName = item.summary.replace('Cuti Bersama', '').trim();
// function ini untuk menyamakan dan menggabungkan Hari Libur
if (eventName == 'Hari Idul Fitri')
eventName = eventName.replace('Hari', '').trim();
else if (eventName == 'Natal (Hari Tinju)' || eventName == 'Malam Natal')
eventName = 'Hari Raya Natal';
const exist = events.find((event) => {
return event.name.toLowerCase().includes(eventName.toLowerCase());
});
if (exist) {
Object.assign(exist, {
end_date: item.start.date,
total_day: exist.total_day + 1,
});
} else {
events.push({
name: eventName,
start_date: item.start?.date,
end_date: item.start?.date,
total_day: 1,
});
}
});
return events;
}
}

View File

@ -0,0 +1,79 @@
import { Injectable } from '@nestjs/common';
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
import { SeasonPeriodEntity } from '../../entities/season-period.entity';
import { SelectQueryBuilder } from 'typeorm';
import {
Param,
RelationParam,
} from 'src/core/modules/domain/entities/base-filter.entity';
@Injectable()
export class IndexSeasonPeriodManager extends BaseIndexManager<SeasonPeriodEntity> {
async prepareData(): Promise<void> {
return;
}
async beforeProcess(): Promise<void> {
return;
}
async afterProcess(): Promise<void> {
return;
}
get relations(): RelationParam {
return {
// relation only join (for query purpose)
joinRelations: ['season_type'],
// relation join and select (relasi yang ingin ditampilkan),
selectRelations: [],
// relation yang hanya ingin dihitung (akan return number)
countRelations: [],
};
}
get selects(): string[] {
return [
`${this.tableName}.id`,
`${this.tableName}.created_at`,
`${this.tableName}.creator_name`,
`${this.tableName}.editor_name`,
`${this.tableName}.updated_at`,
`${this.tableName}.status`,
`${this.tableName}.start_date`,
`${this.tableName}.end_date`,
`${this.tableName}.days`,
`${this.tableName}.holiday_name`,
'season_type.id',
'season_type.name',
];
}
get specificFilter(): Param[] {
return [
{
cols: `${this.tableName}.holiday_name`,
data: this.filterParam.holiday_names,
},
];
}
setQueryFilter(
queryBuilder: SelectQueryBuilder<SeasonPeriodEntity>,
): SelectQueryBuilder<SeasonPeriodEntity> {
if (this.filterParam.start_date && this.filterParam.end_date) {
queryBuilder.andWhere(
`${this.tableName}.start_date BETWEEN :from AND :to`,
{
from: this.filterParam.start_date,
to: this.filterParam.end_date,
},
);
}
return queryBuilder;
}
}

View File

@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { IndexSeasonPeriodManager } from './managers/index-season-period.manager';
import { SeasonPeriodReadService } from '../../data/services/season-period-read.service';
import { SeasonPeriodEntity } from '../entities/season-period.entity';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { BaseReadOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-read.orchestrator';
import { DetailSeasonPeriodManager } from './managers/detail-season-period.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
@Injectable()
export class SeasonPeriodReadOrchestrator extends BaseReadOrchestrator<SeasonPeriodEntity> {
constructor(
private indexManager: IndexSeasonPeriodManager,
private detailManager: DetailSeasonPeriodManager,
private serviceData: SeasonPeriodReadService,
) {
super();
}
async index(params): Promise<PaginationResponse<SeasonPeriodEntity>> {
this.indexManager.setFilterParam(params);
this.indexManager.setService(this.serviceData, TABLE_NAME.SEASON_PERIOD);
await this.indexManager.execute();
return this.indexManager.getResult();
}
async detail(dataId: string): Promise<SeasonPeriodEntity> {
this.detailManager.setData(dataId);
this.detailManager.setService(this.serviceData, TABLE_NAME.SEASON_PERIOD);
await this.detailManager.execute();
return this.detailManager.getResult();
}
}

View File

@ -0,0 +1,28 @@
import { BaseFilterDto } from 'src/core/modules/infrastructure/dto/base-filter.dto';
import { FilterSeasonPeriodEntity } from '../../domain/entities/filter-season-period.entity';
import { EnumDays } from '../../constants';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
export class FilterSeasonPeriodDto
extends BaseFilterDto
implements FilterSeasonPeriodEntity
{
@ApiProperty({ type: Date, required: false })
start_date: Date;
@ApiProperty({ type: Date, required: false })
end_date: Date;
@ApiProperty({ type: ['string'], required: false })
@Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value];
})
holiday_names: string[];
@ApiProperty({ type: ['string'], required: false })
@Transform((body) => {
return Array.isArray(body.value) ? body.value : [body.value];
})
days: EnumDays[];
}

View File

@ -0,0 +1,30 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { FilterSeasonPeriodDto } from './dto/filter-season-period.dto';
import { Pagination } from 'src/core/response';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { SeasonPeriodEntity } from '../domain/entities/season-period.entity';
import { SeasonPeriodReadOrchestrator } from '../domain/usecases/season-period-read.orchestrator';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { Public } from 'src/core/guards';
@ApiTags(`${MODULE_NAME.SEASON_PERIOD.split('-').join(' ')} - read`)
@Controller(MODULE_NAME.SEASON_PERIOD)
@Public(false)
@ApiBearerAuth('JWT')
export class SeasonPeriodReadController {
constructor(private orchestrator: SeasonPeriodReadOrchestrator) {}
@Get()
@Pagination()
async index(
@Query() params: FilterSeasonPeriodDto,
): Promise<PaginationResponse<SeasonPeriodEntity>> {
return await this.orchestrator.index(params);
}
@Get(':id')
async detail(@Param('id') id: string): Promise<SeasonPeriodEntity> {
return await this.orchestrator.detail(id);
}
}