Skip to content

Commit

Permalink
Merge pull request #2402 from HHS/mb/TTAHUB-3452/force-standard-commu…
Browse files Browse the repository at this point in the history
…nication-date

[TTAHUB-3452] Fix comm date validation
  • Loading branch information
thewatermethod authored Oct 15, 2024
2 parents f3230ad + 871d481 commit 58e6f3e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/services/communicationLog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
updateLog,
createLog,
orderLogsBy,
formatCommunicationDateWithJsonData,
} from './communicationLog';
import { createRecipient, createUser } from '../testUtils';

Expand Down Expand Up @@ -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' });
});
});
});
39 changes: 35 additions & 4 deletions src/services/communicationLog.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
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';

const { sequelize, CommunicationLog } = db;

interface CommLogData {
communicationDate?: string;
purpose?: string;
result?: string;
}

interface CommLog {
files: unknown[];
recipientId: number;
userId: number;
id: number;
data: unknown;
data: CommLogData;
authorName: string;
author: {
id: number;
name: string;
}
}

export const formatCommunicationDateWithJsonData = (data: CommLogData): CommLogData => {
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[] => {
Expand Down Expand Up @@ -65,11 +94,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 = {
Expand Down Expand Up @@ -203,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 });
return log.update({ data: formatCommunicationDateWithJsonData(data as CommLogData) });
};

export {
Expand Down

0 comments on commit 58e6f3e

Please sign in to comment.