test for valid date string for creation date

This commit is contained in:
Moriz Wahl 2022-11-10 19:18:01 +01:00
parent 5e2d6c3bc9
commit 2b27e6aaa8
3 changed files with 13 additions and 4 deletions

View File

@ -1066,7 +1066,7 @@ describe('AdminResolver', () => {
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'No information for available creations with the given creationDate=',
new Date(variables.creationDate).toISOString(),
new Date(variables.creationDate).toString(),
)
})
})
@ -1091,7 +1091,7 @@ describe('AdminResolver', () => {
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'No information for available creations with the given creationDate=',
new Date(variables.creationDate).toISOString(),
new Date(variables.creationDate).toString(),
)
})
})
@ -1353,7 +1353,7 @@ describe('AdminResolver', () => {
expect.objectContaining({
errors: [
new GraphQLError(
'The amount (1900 GDD) to be created exceeds the amount (600 GDD) still available for this month.',
'The amount (1900 GDD) to be created exceeds the amount (1000 GDD) still available for this month.',
),
],
}),
@ -1362,7 +1362,7 @@ describe('AdminResolver', () => {
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'The amount (1900 GDD) to be created exceeds the amount (600 GDD) still available for this month.',
'The amount (1900 GDD) to be created exceeds the amount (1000 GDD) still available for this month.',
)
})
})

View File

@ -49,6 +49,7 @@ import {
validateContribution,
isStartEndDateValid,
updateCreations,
isValidDateString,
} from './util/creations'
import {
CONTRIBUTIONLINK_NAME_MAX_CHARS,
@ -237,6 +238,10 @@ export class AdminResolver {
logger.info(
`adminCreateContribution(email=${email}, amount=${amount}, memo=${memo}, creationDate=${creationDate})`,
)
if (!isValidDateString(creationDate)) {
logger.error(`invalid Date for creationDate=${creationDate}`)
throw new Error(`invalid Date for creationDate=${creationDate}`)
}
const emailContact = await UserContact.findOne({
where: { email },
withDeleted: true,

View File

@ -137,3 +137,7 @@ export const updateCreations = (creations: Decimal[], contribution: Contribution
creations[index] = creations[index].plus(contribution.amount.toString())
return creations
}
export const isValidDateString = (dateString: string): boolean => {
return new Date(dateString).toString() !== 'Invalid Date'
}