Fix com_taxa_synonym: when id's were created for a relation table, they were not added to writeList

This commit is contained in:
ShootingStar91
2024-08-25 19:14:18 +03:00
parent 418e0a3256
commit fd7a53c0aa
5 changed files with 46 additions and 20 deletions
+3 -10
View File
@@ -2,7 +2,7 @@ import { Request, Router } from 'express'
import { getAllSpecies, getSpeciesDetails } from '../services/species'
import { fixBigInt } from '../utils/common'
import { EditMetaData, SpeciesDetailsType } from '../../../frontend/src/backendTypes'
import { write } from '../services/write/write'
import { writeSpecies } from '../services/write/species'
const router = Router()
@@ -20,15 +20,8 @@ router.get('/:id', async (req, res) => {
router.put('/', async (req: Request<object, object, { species: SpeciesDetailsType & EditMetaData }>, res) => {
const editedSpecies = req.body.species
const result = await write(
editedSpecies,
'com_species',
req.user!.initials,
editedSpecies.comment ?? '',
editedSpecies.species_id ? 'update' : 'add',
editedSpecies.references ?? []
)
return res.status(200).send(result ? { result: result } : { error: 'error' })
const id = await writeSpecies(editedSpecies)
return res.status(200).send({ id })
})
export default router
+1 -1
View File
@@ -8,7 +8,7 @@ export const writeLocality = async (locality: EditDataType<LocalityDetailsType>)
NOW_DB_NAME,
'now_loc',
'lid',
getFieldsOfTables(['now_loc', 'com_species', 'now_mus', 'now_ss', 'now_coll_meth', 'now_syn_loc'])
getFieldsOfTables(['now_loc', 'now_ls', 'com_species', 'now_mus', 'now_ss', 'now_coll_meth', 'now_syn_loc'])
)
await writeHandler.start()
+31
View File
@@ -0,0 +1,31 @@
import { EditDataType, SpeciesDetailsType } from '../../../../frontend/src/backendTypes'
import { NOW_DB_NAME } from '../../utils/config'
import { WriteHandler } from '../writeOperations/writeHandler'
import { getFieldsOfTables } from '../../utils/db'
export const writeSpecies = async (species: EditDataType<SpeciesDetailsType>) => {
const writeHandler = new WriteHandler(
NOW_DB_NAME,
'com_species',
'species_id',
getFieldsOfTables(['com_species', 'now_ls'])
)
await writeHandler.start()
if (!species.species_id) {
const { species_id: newId } = await writeHandler.createObject('com_species', species, ['species_id'])
species.species_id = newId as number
} else {
await writeHandler.updateObject('com_species', species, ['species_id'])
}
writeHandler.idValue = species.species_id
await writeHandler.upsertList('now_ls', species.now_ls, ['lid', 'species_id'])
await writeHandler.upsertList('com_taxa_synonym', species.com_taxa_synonym, ['synonym_id', 'species_id'])
console.log(JSON.stringify(writeHandler.writeList))
await writeHandler.logUpdates()
await writeHandler.end()
return species.species_id
}
+1 -1
View File
@@ -120,7 +120,7 @@ export const logAllUpdates = async (
await writeUpdateEntries(updateEntries, writeContext.connection, userInitials, comment)
await writeReferences(updateEntries, writeContext.connection, references)
console.log({ writeList })
// The write has value and oldValue swapped for 'delete' type logRows due to reasons.
// It is simpler to fix it here than changing how write works, because values are used to find id's above.
const fixDeleteRows = (logRow: LogRow) => {
@@ -20,13 +20,16 @@ export class WriteHandler extends DatabaseHandler {
this.allowedColumns = allowedColumns
}
async createRow<T extends Record<string, DbValue>>(
table: AllowedTables,
items: DbWriteItem[],
idsToReturn: string[]
) {
async createRow<T extends Record<string, DbValue>>(table: AllowedTables, items: DbWriteItem[], ids: string[]) {
const returnValue = await super.insert<T>(table, items, ids)
// If ID was created, add it to writelist also.
for (const id of ids) {
if (!items.find(item => item.column === id && item.value)) {
items.push({ column: id, value: returnValue[id] })
}
}
this.writeList.push({ table, type: 'add', items: items.map(item => ({ ...item, table })) })
return await super.insert<T>(table, items, idsToReturn)
return returnValue
}
/* Checks each field for their old value and if they did not (meaningfully) change,
@@ -37,13 +40,12 @@ export class WriteHandler extends DatabaseHandler {
async updateRow<T extends Record<string, DbValue>>(table: AllowedTables, items: DbWriteItem[], ids: DbWriteItem[]) {
const oldObject = await super.select<T>(table, [], ids)
const fieldsToWrite: Item[] = []
// TODO put oldValue here too for logging
for (const { column, value } of items) {
const oldValue = oldObject[column as keyof T]
const fixedValue = fixBoolean(value)
if (!valueIsDifferent(fixedValue, oldValue)) continue
console.log({ value: fixedValue, oldValue, column })
fieldsToWrite.push({ column, value: fixedValue, oldValue, table })
}