Add edit action type to writeContext, meaning update/add type is logged correctly

This commit is contained in:
ShootingStar91
2024-08-20 11:58:32 +03:00
parent 20de9e8f01
commit 775afe5fcc
3 changed files with 14 additions and 4 deletions
+7 -1
View File
@@ -35,7 +35,13 @@ router.put(
return res.status(400).send({ validationErrors })
}
const result = await write(editedLocality, 'now_loc', req.user!.initials, comment ?? '')
const result = await write(
editedLocality,
'now_loc',
req.user!.initials,
comment ?? '',
editedLocality.lid ? 'update' : 'add'
)
return res.status(200).send(result ? { id: result } : { error: 'error' })
}
)
+4 -1
View File
@@ -1,6 +1,7 @@
import { pool } from '../../utils/db'
import { logger } from '../../utils/logger'
import {
ActionType,
AllowedTables,
CustomObject,
DbValue,
@@ -31,6 +32,7 @@ export type WriteContext = {
connection: PoolConnection
writeList: WriteItem[]
username: string
type: ActionType
}
const writeTable = async <T extends CustomObject>(obj: T, tableName: AllowedTables, writeContext: WriteContext) => {
@@ -163,7 +165,7 @@ const writeTable = async <T extends CustomObject>(obj: T, tableName: AllowedTabl
return id
}
export const write: WriteFunction = async (data, tableName, userInitials, comment) => {
export const write: WriteFunction = async (data, tableName, userInitials, comment, type) => {
const connection = await pool.getConnection()
await connection.beginTransaction()
@@ -171,6 +173,7 @@ export const write: WriteFunction = async (data, tableName, userInitials, commen
connection,
writeList: [],
username: userInitials,
type,
}
try {
+3 -2
View File
@@ -72,7 +72,7 @@ export const logAllUpdates = async (
}
const rootUpdateEntry: UpdateEntry = {
type: 'add', // TODO fix: this should probably be received as an argument?
type: writeContext.type,
table: tableName,
id,
logRows: writeList.flatMap(writeItem =>
@@ -280,7 +280,8 @@ export type WriteFunction = <T extends CustomObject>(
data: T,
tableName: PrimaryTables,
username: string,
comment: string
comment: string,
type: ActionType
) =>
| Promise<string | number>
| ((data: EditDataType<ReferenceDetailsType>, tableName: 'ref_ref') => Promise<string | number>)