Fix com_species changes shown in now_loc update

This commit is contained in:
ShootingStar91
2024-08-29 22:41:46 +03:00
parent 5e0f54a0da
commit 7df77af86f
3 changed files with 36 additions and 13 deletions
@@ -2,7 +2,7 @@ import { Reference } from '../../../../../frontend/src/backendTypes'
import { COORDINATOR } from '../../../utils/config'
import { AllowedTables, ActionType, Item, LogRow, PrimaryTables, WriteItem, UpdateEntry } from '../types'
import { WriteHandler } from '../writeHandler'
import { prefixToIdColumn, primaryTableToUpdatePrefix, tableToUpdateTargets } from './utils'
import { filterRelevantLogRows, prefixToIdColumn, primaryTableToUpdatePrefix, tableToUpdateTargets } from './utils'
import { createUpdateEntry, writeLogRows, writeReferences } from './writeUpdates'
export const logAllUpdates = async (
@@ -39,11 +39,13 @@ export const logAllUpdates = async (
table: tableName,
id,
logRows: writeList.flatMap(writeItem =>
writeItem.items.map(item => ({
...item,
type: writeItem.type,
pkData: `${id.toString().length}.${id};${getSecondaryPkData(writeItem.table, writeItem.items)}`,
}))
writeItem.items
.filter(item => filterRelevantLogRows(item, tableName))
.map(item => ({
...item,
type: writeItem.type,
pkData: `${id.toString().length}.${id};${getSecondaryPkData(writeItem.table, writeItem.items)}`,
}))
),
}
@@ -65,11 +67,21 @@ export const logAllUpdates = async (
throw new Error(`Error when creating update entries: id not found for targetTable: ${targetTable}`)
if (!secondaryUpdateEntries[targetTable]) secondaryUpdateEntries[targetTable] = {}
const pkData = `${id.toString().length}.${id};${secondaryId.toString().length}.${secondaryId};`
secondaryUpdateEntries[targetTable][secondaryId] = {
id: secondaryId,
table: targetTable,
type: writeListItem.type,
logRows: writeListItem.items.map(item => ({ ...item, pkData, type: writeListItem.type })),
if (secondaryUpdateEntries[targetTable][secondaryId]) {
secondaryUpdateEntries[targetTable][secondaryId] = {
...secondaryUpdateEntries[targetTable][secondaryId],
logRows: [
...secondaryUpdateEntries[targetTable][secondaryId].logRows,
...writeListItem.items.map(item => ({ ...item, pkData, type: writeListItem.type })),
],
}
} else {
secondaryUpdateEntries[targetTable][secondaryId] = {
id: secondaryId,
table: targetTable,
type: writeListItem.type,
logRows: writeListItem.items.map(item => ({ ...item, pkData, type: writeListItem.type })),
}
}
}
}
@@ -1,4 +1,4 @@
import { AllowedTables, PrimaryTables } from '../types'
import { AllowedTables, Item, PrimaryTables } from '../types'
export const updateTableToIdColumn: Record<string, string> = {
now_lau: 'lid',
@@ -30,4 +30,15 @@ export const primaryTableToUpdatePrefix = {
table, assume it goes to the "main" update entry e.g. if user edited locality, now_loc. */
export const tableToUpdateTargets = {
now_ls: ['now_loc', 'com_species'],
com_species: ['com_species'], // This is because com_species may be updated through now_loc
} as Record<AllowedTables, PrimaryTables[] | undefined>
/*
This returns false for any logRow that should not be shown in the updates of tableName.
For example, a com_species entry can be created while editing now_loc, but we don't want
to show it in the now_loc update logs, only the now_ls part.
*/
export const filterRelevantLogRows = (item: Item, tableName: PrimaryTables) => {
if (tableName === 'now_loc' && item.table === 'com_species') return false
return true
}
+1 -1
View File
@@ -46,7 +46,7 @@ export type LocalitySpecies = FixBigInt<Prisma.now_ls> & { com_species: SpeciesT
export type LocalitySpeciesDetailsType = FixBigInt<Prisma.now_ls> & { com_species: SpeciesDetailsType }
export type SpeciesLocality = FixBigInt<Prisma.now_ls> & { now_loc: Prisma.now_loc }
export type LocalityUpdate = Prisma.now_lau & { now_lr: LocalityReference[] } & { updates: UpdateLog[] }
export type SpeciesUpdate = Prisma.now_sau & { now_sr: Prisma.now_sr }
export type SpeciesUpdate = Prisma.now_sau & { now_sr: Prisma.now_sr } & { updates: UpdateLog[] }
export type Museum = Prisma.com_mlist
export type PersonDetailsType = Prisma.com_people & { user: Omit<Prisma.com_users, 'password, newpassword'> | null }
export type ProjectPeople = Prisma.now_proj_people