Skip to content

Commit b42ec25

Browse files
authored
fix(analytics): clear existing event_actions before upsert and mark last action as accepted on immediate confirmation (#1094)
1 parent bd099fa commit b42ec25

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/analytics/analytics.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,16 @@ function convertDotKeysToUnderscore(
136136
return newObj
137137
}
138138

139-
export async function upsertAnalyticsEventActions(
139+
async function upsertAnalyticsEventActions(
140140
event: EventDocument,
141141
eventConfig: EventConfig,
142142
trx: Kysely<any>
143143
) {
144+
await trx
145+
.deleteFrom('analytics.event_actions')
146+
.where('event_id', '=', event.id)
147+
.execute()
148+
144149
for (let i = 0; i < event.actions.length; i++) {
145150
const actionsFromStartToCurrentPoint = event.actions
146151
.sort((a, b) => {

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ import {
6363
onAnyActionHandler
6464
} from '@countryconfig/api/custom-event/handler'
6565
import { readFileSync } from 'fs'
66-
import { ActionType, EventDocument } from '@opencrvs/toolkit/events'
66+
import {
67+
ActionDocument,
68+
ActionStatus,
69+
ActionType,
70+
EventDocument
71+
} from '@opencrvs/toolkit/events'
6772
import { Event } from './form/types/types'
6873
import { onRegisterHandler } from './api/registration'
6974
import { workqueueconfigHandler } from './api/workqueue/handler'
@@ -279,6 +284,7 @@ export async function createServer() {
279284
server.route({
280285
method: 'GET',
281286
path: '/ping',
287+
// eslint-disable-next-line no-unused-vars
282288
handler: (request: any, h: any) => {
283289
// Perform any health checks and return true or false for success prop
284290
return {
@@ -704,10 +710,20 @@ export async function createServer() {
704710

705711
if (wasRequestForActionConfirmation && wasActionAcceptedImmediately) {
706712
const event = request.payload as EventDocument
713+
714+
const eventWithOptimisticallyApprovedLastAction = {
715+
...event,
716+
actions: event.actions.map((action, index) =>
717+
index === event.actions.length - 1
718+
? { ...action, status: ActionStatus.Accepted }
719+
: action
720+
) as ActionDocument[]
721+
}
722+
707723
const client = getClient()
708724
try {
709725
await client.transaction().execute(async (trx) => {
710-
await importEvent(event, trx)
726+
await importEvent(eventWithOptimisticallyApprovedLastAction, trx)
711727
})
712728
} catch (error) {
713729
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)