From 05d62d1bf08594e94a1c379aff99a4bd2d5cf495 Mon Sep 17 00:00:00 2001 From: Matt Bevilacqua Date: Tue, 8 Oct 2024 13:25:52 -0400 Subject: [PATCH 1/2] Fix comm date validation --- src/services/communicationLog.test.js | 27 +++++++++++++++++++ src/services/communicationLog.ts | 37 ++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/services/communicationLog.test.js b/src/services/communicationLog.test.js index c53337d9ab..687636bd0a 100644 --- a/src/services/communicationLog.test.js +++ b/src/services/communicationLog.test.js @@ -9,6 +9,7 @@ import { updateLog, createLog, orderLogsBy, + formatCommunicationDateWithJsonData, } from './communicationLog'; import { createRecipient, createUser } from '../testUtils'; @@ -144,4 +145,30 @@ describe('communicationLog services', () => { expect(result).toEqual([['data.communicationDate', 'asc']]); }); }); + + describe('formatCommunicationDateWithJsonData', () => { + const badDatesLong = [ + '08/16/24', + '8/16/24', + '8/16/2024', + '08/16//24', + '8-16-24', + '08/16/20240.75', + ]; + + const badDatesShort = [ + '08/4/2024', + '8/4/24', + '8/4/2024', + '08/4/24', + ]; + + it.each(badDatesLong)('should return 08/16/2024 when the date is %s', (date) => { + expect(formatCommunicationDateWithJsonData({ communicationDate: date })).toEqual({ communicationDate: '08/16/2024' }); + }); + + it.each(badDatesShort)('should return 08/04/2024 when the date is %s', (date) => { + expect(formatCommunicationDateWithJsonData({ communicationDate: date })).toEqual({ communicationDate: '08/04/2024' }); + }); + }); }); diff --git a/src/services/communicationLog.ts b/src/services/communicationLog.ts index 0807316efb..6da64c655e 100644 --- a/src/services/communicationLog.ts +++ b/src/services/communicationLog.ts @@ -1,5 +1,6 @@ import { WhereOptions, Op } from 'sequelize'; import stringify from 'csv-stringify/lib/sync'; +import moment from 'moment'; import db from '../models'; import { communicationLogToCsvRecord } from '../lib/transform'; @@ -10,7 +11,11 @@ interface CommLog { recipientId: number; userId: number; id: number; - data: unknown; + data: { + communicationDate: string; + purpose: string; + result: string; + }; authorName: string; author: { id: number; @@ -18,6 +23,28 @@ interface CommLog { } } +export const formatCommunicationDateWithJsonData = (data: { communicationDate: string }) => { + if (data.communicationDate) { + const formattedCommunicationDate = moment(data.communicationDate, 'MM/DD/YYYY').format('MM/DD/YYYY'); + + if (formattedCommunicationDate === 'Invalid date') { + return { + ...data, + communicationDate: '', + }; + } + + if (formattedCommunicationDate !== data.communicationDate) { + return { + ...data, + communicationDate: formattedCommunicationDate, + }; + } + } + + return data; +}; + const COMMUNICATION_LOGS_PER_PAGE = 10; export const orderLogsBy = (sortBy: string, sortDir: string): string[] => { @@ -65,11 +92,13 @@ export const orderLogsBy = (sortBy: string, sortDir: string): string[] => { const createLog = async ( recipientId: number, userId: number, - data: unknown, + data: { + communicationDate: string; + }, ) => CommunicationLog.create({ recipientId, userId, - data, + data: formatCommunicationDateWithJsonData(data), }); const LOG_INCLUDE_ATTRIBUTES = { @@ -203,7 +232,7 @@ const updateLog = async (id: number, logData: CommLog) => { ...data } = logData; const log = await CommunicationLog.findOne(LOG_WHERE_OPTIONS(id)); - return log.update({ data }); + return log.update({ data: formatCommunicationDateWithJsonData(data) }); }; export { From 871d481a60344837813587cf78dbf89738ccdc70 Mon Sep 17 00:00:00 2001 From: Matt Bevilacqua Date: Tue, 8 Oct 2024 13:52:08 -0400 Subject: [PATCH 2/2] Clean up typescript --- src/services/communicationLog.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/services/communicationLog.ts b/src/services/communicationLog.ts index 6da64c655e..363040f61c 100644 --- a/src/services/communicationLog.ts +++ b/src/services/communicationLog.ts @@ -6,16 +6,18 @@ import { communicationLogToCsvRecord } from '../lib/transform'; const { sequelize, CommunicationLog } = db; +interface CommLogData { + communicationDate?: string; + purpose?: string; + result?: string; +} + interface CommLog { files: unknown[]; recipientId: number; userId: number; id: number; - data: { - communicationDate: string; - purpose: string; - result: string; - }; + data: CommLogData; authorName: string; author: { id: number; @@ -23,7 +25,7 @@ interface CommLog { } } -export const formatCommunicationDateWithJsonData = (data: { communicationDate: string }) => { +export const formatCommunicationDateWithJsonData = (data: CommLogData): CommLogData => { if (data.communicationDate) { const formattedCommunicationDate = moment(data.communicationDate, 'MM/DD/YYYY').format('MM/DD/YYYY'); @@ -232,7 +234,7 @@ const updateLog = async (id: number, logData: CommLog) => { ...data } = logData; const log = await CommunicationLog.findOne(LOG_WHERE_OPTIONS(id)); - return log.update({ data: formatCommunicationDateWithJsonData(data) }); + return log.update({ data: formatCommunicationDateWithJsonData(data as CommLogData) }); }; export {