Skip to content

Commit

Permalink
Fix last update by field
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Nov 4, 2024
1 parent 7c57185 commit bd6f100
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,14 @@ class CreateOrUpdateManualPriorNotification(
val allPorts = portRepository.findAll()

val authorTrigram = existingMessageValue?.authorTrigram
val createdBy = existingMessageValue?.createdBy ?: author
val createdBy = existingMessageValue?.createdBy ?: existingMessageValue?.authorTrigram ?: author
val isInVerificationScope =
ManualPriorNotificationComputedValues
.isInVerificationScope(computedVesselFlagCountryCode, computedVesselRiskFactor)
// If the prior notification is not in verification scope,
// we pass `isBeingSent` as `true` in order to ask the workflow to send it.
val isBeingSent = !isInVerificationScope && isPartOfControlUnitSubscriptions
val portName = allPorts.find { it.locode == portLocode }?.name
val updatedBy = if (existingMessageValue != null) author else null
val updatedAt = if (existingMessageValue != null) ZonedDateTime.now() else null

return PNO().apply {
this.authorTrigram = authorTrigram
Expand Down Expand Up @@ -231,8 +229,8 @@ class CreateOrUpdateManualPriorNotification(
this.statisticalRectangle = null
this.tripStartDate = null
this.riskFactor = computedVesselRiskFactor
this.updatedBy = updatedBy
this.updatedAt = updatedAt
this.updatedBy = author
this.updatedAt = ZonedDateTime.now()
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/cypress/e2e/utils/isDateCloseTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function isDateCloseTo(
thresholdInSeconds: number
): boolean {
const leftDateAsDayjs: Dayjs = isDayjs(leftDate) ? leftDate : dayjs(leftDate)
const rightDateAsDayjs: Dayjs = isDayjs(rightDate) ? rightDate : dayjs(leftDate)
const rightDateAsDayjs: Dayjs = isDayjs(rightDate) ? rightDate : dayjs(rightDate)

return Math.abs(leftDateAsDayjs.diff(rightDateAsDayjs, 'second')) <= thresholdInSeconds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { isDateCloseTo } from '@features/PriorNotification/components/shared/EditHistory/utils'
import { describe, expect, it } from '@jest/globals'
import dayjs from 'dayjs'

describe('features/PriorNotificationList/shared/EditHistory/utils', () => {
it('isDateCloseTo() should return true when dates are within the threshold', () => {
// Given
const leftDate = dayjs('2024-01-01T10:00:00')
const rightDate = dayjs('2024-01-01T10:00:10')
const thresholdInSeconds = 10

// When Then
expect(isDateCloseTo(leftDate, rightDate, thresholdInSeconds)).toBe(true)
})

it('isDateCloseTo() should return false when dates exceed the threshold', () => {
// Given
const leftDate = dayjs('2024-01-01T10:00:00')
const rightDate = dayjs('2024-01-01T10:01:00')
const thresholdInSeconds = 30

// When Then
expect(isDateCloseTo(leftDate, rightDate, thresholdInSeconds)).toBe(false)
})

it('isDateCloseTo() should handle different date formats (string, Date, Dayjs) and return true for close dates', () => {
// Given
const leftDate = '2024-01-01T10:00:00'
const rightDate = new Date('2024-01-01T10:00:05')
const thresholdInSeconds = 10

// When Then
expect(isDateCloseTo(leftDate, rightDate, thresholdInSeconds)).toBe(true)
})

it('isDateCloseTo() should handle different date formats (string, Date, Dayjs) and return false for far dates', () => {
// Given
const leftDate = new Date('2024-01-01T10:00:00')
const rightDate = dayjs('2024-01-01T10:01:00')
const thresholdInSeconds = 30

// When Then
expect(isDateCloseTo(leftDate, rightDate, thresholdInSeconds)).toBe(false)
})

it('isDateCloseTo() should return true when dates are exactly at the threshold limit', () => {
// Given
const leftDate = dayjs('2024-01-01T10:00:00')
const rightDate = dayjs('2024-01-01T10:01:00')
const thresholdInSeconds = 60

// When Then
expect(isDateCloseTo(leftDate, rightDate, thresholdInSeconds)).toBe(true)
})

it('isDateCloseTo() should return true for identical dates with any threshold', () => {
// Given
const date = dayjs('2024-01-01T10:00:00')
const thresholdInSeconds = 0

// When Then
expect(isDateCloseTo(date, date, thresholdInSeconds)).toBe(true)
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { customDayjs } from '@mtes-mct/monitor-ui'
import dayjs, { isDayjs } from 'dayjs'

import type { PriorNotification } from '@features/PriorNotification/PriorNotification.types'
import type { Dayjs } from 'dayjs'

function getCreatedByLabel(priorNotificationDetail: PriorNotification.Detail) {
if (!priorNotificationDetail.isManuallyCreated) {
Expand All @@ -26,7 +28,7 @@ export function getCreationLabel(priorNotificationDetail: PriorNotification.Deta
}

export function getLasUpdateLabel(priorNotificationDetail: PriorNotification.Detail) {
if (priorNotificationDetail.updatedAt === priorNotificationDetail.createdAt) {
if (isDateCloseTo(priorNotificationDetail.createdAt, priorNotificationDetail.updatedAt, 1)) {
return undefined
}

Expand All @@ -42,3 +44,14 @@ export function getLasUpdateLabel(priorNotificationDetail: PriorNotification.Det
.join(' ')
.concat('.')
}

export function isDateCloseTo(
leftDate: string | Date | Dayjs,
rightDate: string | Date | Dayjs,
thresholdInSeconds: number
): boolean {
const leftDateAsDayjs: Dayjs = isDayjs(leftDate) ? leftDate : dayjs(leftDate)
const rightDateAsDayjs: Dayjs = isDayjs(rightDate) ? rightDate : dayjs(rightDate)

return Math.abs(leftDateAsDayjs.diff(rightDateAsDayjs, 'second')) <= thresholdInSeconds
}

0 comments on commit bd6f100

Please sign in to comment.