From ad9ccf13ba1d8af93bd7cb6baa3862c99825a845 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 18 Jul 2024 16:53:54 +0300 Subject: [PATCH] bugfix: [#269] Volunteering hours set on a date appear on the following one (#299) --- backend/src/common/constants/constants.ts | 2 +- .../base/repository-with-pagination.class.ts | 16 +++++++++++++--- ...119397821-ChangeActivityLogDatetimeToDate.ts | 15 +++++++++++++++ .../entities/activity-log.entity.ts | 2 +- .../repositories/activity-log.repository.ts | 2 ++ .../services/activity-log/activity-log.api.ts | 17 +++++++++++------ .../services/activity-log/activity-log.api.ts | 6 +++--- 7 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 backend/src/migrations/1721119397821-ChangeActivityLogDatetimeToDate.ts diff --git a/backend/src/common/constants/constants.ts b/backend/src/common/constants/constants.ts index 388ec280d..fafe66b50 100644 --- a/backend/src/common/constants/constants.ts +++ b/backend/src/common/constants/constants.ts @@ -6,7 +6,7 @@ export const QUEUES: Record = { }; export const DATE_CONSTANTS = { - YYYY_MM_DD_HH_SS: 'yyyy-MM-dd HH:MM:SS', + YYYY_MM_DD_HH_SS: 'yyyy-MM-dd HH:mm:ss', YYYY_MM_DD: 'yyyy-MM-dd', }; diff --git a/backend/src/infrastructure/base/repository-with-pagination.class.ts b/backend/src/infrastructure/base/repository-with-pagination.class.ts index 9fdd93d6c..3df34e602 100644 --- a/backend/src/infrastructure/base/repository-with-pagination.class.ts +++ b/backend/src/infrastructure/base/repository-with-pagination.class.ts @@ -86,16 +86,26 @@ export abstract class RepositoryWithPagination column: string, start: Date, end?: Date, + customDateFormat?: string, ): SelectQueryBuilder { const prefix = column.split('.').join(''); if (end) { query.andWhere(`${column} BETWEEN :${prefix}Start AND :${prefix}End`, { - [`${prefix}Start`]: format(start, DATE_CONSTANTS.YYYY_MM_DD_HH_SS), - [`${prefix}End`]: format(end, DATE_CONSTANTS.YYYY_MM_DD_HH_SS), + [`${prefix}Start`]: format( + start, + customDateFormat || DATE_CONSTANTS.YYYY_MM_DD_HH_SS, + ), + [`${prefix}End`]: format( + end, + customDateFormat || DATE_CONSTANTS.YYYY_MM_DD_HH_SS, + ), }); } else { query.andWhere(`${column} >= :${prefix}Start`, { - [`${prefix}Start`]: format(start, DATE_CONSTANTS.YYYY_MM_DD), + [`${prefix}Start`]: format( + start, + customDateFormat || DATE_CONSTANTS.YYYY_MM_DD, + ), }); } diff --git a/backend/src/migrations/1721119397821-ChangeActivityLogDatetimeToDate.ts b/backend/src/migrations/1721119397821-ChangeActivityLogDatetimeToDate.ts new file mode 100644 index 000000000..a879dabbc --- /dev/null +++ b/backend/src/migrations/1721119397821-ChangeActivityLogDatetimeToDate.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class Migrations1721119397821 implements MigrationInterface { + name = 'ChangeActivityLogDatetimeToDate1721119397821'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "activity_log" ALTER COLUMN date TYPE DATE USING date::DATE;`, + ); + } + + public async down(): Promise { + // Nothing we can do + } +} diff --git a/backend/src/modules/activity-log/entities/activity-log.entity.ts b/backend/src/modules/activity-log/entities/activity-log.entity.ts index b285bb8a2..6eb39f349 100644 --- a/backend/src/modules/activity-log/entities/activity-log.entity.ts +++ b/backend/src/modules/activity-log/entities/activity-log.entity.ts @@ -19,7 +19,7 @@ export class ActivityLogEntity extends BaseEntity { @PrimaryGeneratedColumn('uuid') id: string; - @Column({ type: 'timestamptz', name: 'date' }) + @Column({ type: 'date', name: 'date' }) date: Date; @Column({ type: 'integer', name: 'hours' }) diff --git a/backend/src/modules/activity-log/repositories/activity-log.repository.ts b/backend/src/modules/activity-log/repositories/activity-log.repository.ts index b60bfe51f..481893f99 100644 --- a/backend/src/modules/activity-log/repositories/activity-log.repository.ts +++ b/backend/src/modules/activity-log/repositories/activity-log.repository.ts @@ -21,6 +21,7 @@ import { IActivityLogModel, UpdateActivityLogOptions, } from '../models/activity-log.model'; +import { DATE_CONSTANTS } from 'src/common/constants/constants'; @Injectable() export class ActivityLogRepositoryService @@ -152,6 +153,7 @@ export class ActivityLogRepositoryService 'activityLog.date', findOptions.executionDateStart, findOptions.executionDateEnd, + DATE_CONSTANTS.YYYY_MM_DD, ); } diff --git a/frontend/src/services/activity-log/activity-log.api.ts b/frontend/src/services/activity-log/activity-log.api.ts index 42993cb42..c57113f21 100644 --- a/frontend/src/services/activity-log/activity-log.api.ts +++ b/frontend/src/services/activity-log/activity-log.api.ts @@ -9,6 +9,7 @@ import { IPaginatedEntity } from '../../common/interfaces/paginated-entity.inter import { formatEndDateISO9075, formatStartDateISO9075 } from '../../common/utils/utils'; import { ActivityLogFormTypes } from '../../components/ActivityLogForm'; import API from '../api'; +import { format } from 'date-fns'; export interface GetActivityLogsParams { limit: number; @@ -37,9 +38,9 @@ export const getActivityLogs = async ({ params: { ...params, ...(executionDateStart - ? { executionDateStart: formatStartDateISO9075(executionDateStart) } + ? { executionDateStart: format(executionDateStart, 'yyyy-MM-dd') } : {}), - ...(executionDateEnd ? { executionDateEnd: formatEndDateISO9075(executionDateEnd) } : {}), + ...(executionDateEnd ? { executionDateEnd: format(executionDateEnd, 'yyyy-MM-dd') } : {}), ...(registrationDateStart ? { registrationDateStart: formatStartDateISO9075(registrationDateStart) } : {}), @@ -76,8 +77,12 @@ export const getActivityLogsForDownload = async ({ search, status, approvedOrRejectedBy, - executionDateStart, - executionDateEnd, + executionDateStart: executionDateStart + ? format(executionDateStart, 'yyyy-MM-dd') + : executionDateStart, + executionDateEnd: executionDateEnd + ? format(executionDateEnd, 'yyyy-MM-dd') + : executionDateEnd, volunteerId, }, responseType: 'arraybuffer', @@ -120,7 +125,7 @@ const formatAddActivityLogPayload = (data: ActivityLogFormTypes): object => { const { volunteer, task, event, ...payload } = data; return { ...payload, - date: formatEndDateISO9075(payload.date), + date: format(payload.date, 'yyyy-MM-dd'), volunteerId: volunteer.value, ...(task.value !== CONSTANTS.OTHER ? { activityTypeId: task.value } : {}), ...(event ? { eventId: event.value } : {}), @@ -133,7 +138,7 @@ const formatEditActivityLogPayload = (data: ActivityLogFormTypes): object => { return { ...payload, - date: formatEndDateISO9075(payload.date), + date: format(payload.date, 'yyyy-MM-dd'), ...(task.value !== CONSTANTS.OTHER ? { activityTypeId: task.value } : {}), ...(event ? { eventId: event.value } : {}), }; diff --git a/mobile/src/services/activity-log/activity-log.api.ts b/mobile/src/services/activity-log/activity-log.api.ts index 5f2c2759a..6a4aa8dc3 100644 --- a/mobile/src/services/activity-log/activity-log.api.ts +++ b/mobile/src/services/activity-log/activity-log.api.ts @@ -1,4 +1,4 @@ -import { endOfDay, formatISO9075 } from 'date-fns'; +import { format } from 'date-fns'; import { ActivityLogStatus } from '../../common/enums/activity-log.status.enum'; import { OrderDirection } from '../../common/enums/order-direction.enum'; import { IActivityLogCounters } from '../../common/interfaces/activity-log-counters.interface'; @@ -21,7 +21,7 @@ export const createActivityLog = async (payload: ActivityLogFormTypes): Promise< return API.post('/mobile/activity-log', { ...log, ...(activityTypeId !== CONSTANTS.OTHER_OPTION ? { activityTypeId } : {}), - date: formatISO9075(endOfDay(payload.date)), + date: format(payload.date, 'yyyy-MM-dd'), }).then((res) => res.data); }; @@ -33,7 +33,7 @@ export const updateActivityLog = async ( return API.patch(`/mobile/activity-log/${volunteerId}`, { ...log, ...(activityTypeId !== CONSTANTS.OTHER_OPTION ? { activityTypeId } : {}), - ...(updates.date ? { date: formatISO9075(endOfDay(updates.date)) } : {}), + ...(updates.date ? { date: format(updates.date, 'yyyy-MM-dd') } : {}), }).then((res) => res.data); };