Skip to content

Commit

Permalink
fix: conversion from string date to string iso date (#1231)
Browse files Browse the repository at this point in the history
* fix: conversion from string date to string iso date

* fix: avoid trying to airdrop when badge spec upload went wrong

* fix: use utc time for string conversion
  • Loading branch information
1emu authored Sep 4, 2023
1 parent e69ec14 commit 96e4683
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/back/jobs/BadgeAirdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export async function giveTopVoterBadges() {
const { status, badgeCid, badgeTitle, error } = await BadgesService.createTopVotersBadgeSpec()
if (error && status === ActionStatus.Failed) {
ErrorService.report(error, { category: ErrorCategory.Badges, badgeTitle, badgeCid })
return
}
if (!badgeCid) {
ErrorService.report('Unable to create top voters badge', { category: ErrorCategory.Badges, badgeTitle })
return
}
await BadgesService.queueTopVopVoterAirdrops(badgeCid!)
}
9 changes: 2 additions & 7 deletions src/entities/Badges/storeBadgeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RAFT_OWNER_PK,
TRIMMED_OTTERSPACE_RAFT_ID,
} from '../../constants'
import { toIsoStringDate } from '../../utils/date/toIsoString'

import { ActionStatus, BadgeCreationResult } from './types'

Expand Down Expand Up @@ -44,12 +45,6 @@ const getImageFileFromUrl = async (imgUrl: string) => {
}
}

function convertToISODate(dateString: string): string {
const [year, month, day] = dateString.split('-')
const isoDateString = `${year}-${month}-${day}T00:00:00.000Z`
return isoDateString
}

interface BadgeSpec {
title: string
description: string
Expand All @@ -70,7 +65,7 @@ export async function storeBadgeSpec({ title, description, imgUrl, expiresAt }:
raftTokenId: TRIMMED_OTTERSPACE_RAFT_ID,
raftContractAddress: POLYGON_RAFTS_CONTRACT_ADDRESS,
createdByAddress: raftOwner.address,
expiresAt: expiresAt ? convertToISODate(expiresAt) : '',
expiresAt: expiresAt ? toIsoStringDate(expiresAt) : '',
},
image: file,
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils/date/toIsoString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { toIsoStringDate } from './toIsoString'

describe('convertToISO function', () => {
it('should convert a valid date string to ISO format', () => {
const dateStr = '2023-09-04'
const isoDate = toIsoStringDate(dateStr)
expect(isoDate).toEqual('2023-09-04T00:00:00.000Z')
})

it('should throw an error for an invalid date string', () => {
const invalidDateStr = 'invalid-date'
expect(() => toIsoStringDate(invalidDateStr)).toThrow('Invalid date format')
})

it('should not change the hours and minutes if they are already defined', () => {
const dateStr = '2023-07-21T13:57:28.882Z'
const isoDate = toIsoStringDate(dateStr)
expect(isoDate).toEqual(dateStr)
})

it('should add the missing part of the date string', () => {
const dateStr = '2023-07-21T13:57'
const isoDate = toIsoStringDate(dateStr)
expect(isoDate).toEqual('2023-07-21T13:57:00.000Z')
})

it('should not change the minutes and seconds if they are already defined', () => {
const dateStr = '2023-09-30T23:59:59.999Z'
const isoDate = toIsoStringDate(dateStr)
expect(isoDate).toEqual('2023-09-30T23:59:59.999Z')
})
})
9 changes: 9 additions & 0 deletions src/utils/date/toIsoString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Time from './Time'

export function toIsoStringDate(dateStr: string) {
const parsedDate = Time.utc(dateStr)
if (!parsedDate.isValid()) {
throw new Error('Invalid date format')
}
return parsedDate.toISOString()
}

0 comments on commit 96e4683

Please sign in to comment.