mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
Fix com_taxa_synonym: when id's were created for a relation table, they were not added to writeList
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user