mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
Add species delete route & service
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 { writeSpecies } from '../services/write/species'
|
||||
import { deleteSpecies, writeSpecies } from '../services/write/species'
|
||||
import { requireOneOf } from '../middlewares/authorizer'
|
||||
import { Role } from '../../../frontend/src/types'
|
||||
|
||||
@@ -17,7 +17,7 @@ router.get('/:id', async (req, res) => {
|
||||
const id = parseInt(req.params.id)
|
||||
const species = await getSpeciesDetails(id)
|
||||
if (!species) return res.status(404).send()
|
||||
return res.status(200).send(fixBigInt(species))
|
||||
return res.status(200).send(species)
|
||||
})
|
||||
|
||||
router.put(
|
||||
@@ -30,4 +30,10 @@ router.put(
|
||||
}
|
||||
)
|
||||
|
||||
router.delete('/:id', requireOneOf([Role.Admin, Role.EditUnrestricted]), async (req, res) => {
|
||||
const id = parseInt(req.params.id)
|
||||
await deleteSpecies(id, req.user!)
|
||||
res.status(200).send()
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { SpeciesDetailsType } from '../../../frontend/src/backendTypes'
|
||||
import { fixBigInt } from '../utils/common'
|
||||
import { logDb, nowDb } from '../utils/db'
|
||||
|
||||
export const getAllSpecies = async () => {
|
||||
@@ -59,5 +61,5 @@ export const getSpeciesDetails = async (id: number) => {
|
||||
updates: logResult.filter(logRow => logRow.suid === sau.suid),
|
||||
}))
|
||||
|
||||
return { ...result, com_taxa_synonym: synonyms || [] }
|
||||
return JSON.parse(fixBigInt({ ...result, com_taxa_synonym: synonyms || [] })!) as SpeciesDetailsType
|
||||
}
|
||||
|
||||
@@ -1,7 +1,20 @@
|
||||
import { EditDataType, Reference, SpeciesDetailsType } from '../../../../frontend/src/backendTypes'
|
||||
import { EditDataType, FixBigInt, Reference, SpeciesDetailsType, User } from '../../../../frontend/src/backendTypes'
|
||||
import { NOW_DB_NAME } from '../../utils/config'
|
||||
import { WriteHandler } from '../writeOperations/writeHandler'
|
||||
import { getFieldsOfTables } from '../../utils/db'
|
||||
import { ActionType } from '../writeOperations/types'
|
||||
import { getSpeciesDetails } from '../species'
|
||||
import { makeListRemoved } from '../writeOperations/utils'
|
||||
|
||||
const getSpeciesWriteHandler = (type: ActionType) => {
|
||||
return new WriteHandler({
|
||||
dbName: NOW_DB_NAME,
|
||||
table: 'com_species',
|
||||
idColumn: 'species_id',
|
||||
allowedColumns: getFieldsOfTables(['com_species', 'now_ls']),
|
||||
type: type,
|
||||
})
|
||||
}
|
||||
|
||||
export const writeSpecies = async (
|
||||
species: EditDataType<SpeciesDetailsType>,
|
||||
@@ -9,14 +22,7 @@ export const writeSpecies = async (
|
||||
references: Reference[] | undefined,
|
||||
authorizer: string
|
||||
) => {
|
||||
const writeHandler = new WriteHandler({
|
||||
dbName: NOW_DB_NAME,
|
||||
table: 'com_species',
|
||||
idColumn: 'species_id',
|
||||
allowedColumns: getFieldsOfTables(['com_species', 'now_ls']),
|
||||
type: species.species_id ? 'update' : 'add',
|
||||
})
|
||||
|
||||
const writeHandler = getSpeciesWriteHandler(species.species_id ? 'update' : 'add')
|
||||
try {
|
||||
await writeHandler.start()
|
||||
|
||||
@@ -40,3 +46,28 @@ export const writeSpecies = async (
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteSpecies = async (species_id: number, user: User) => {
|
||||
const species = (await getSpeciesDetails(species_id)) as EditDataType<FixBigInt<SpeciesDetailsType>>
|
||||
species.now_sau[0].updates
|
||||
if (!species) throw new Error('Species not found')
|
||||
|
||||
const writeHandler = getSpeciesWriteHandler('delete')
|
||||
await writeHandler.start()
|
||||
writeHandler.idValue = species.species_id
|
||||
|
||||
makeListRemoved(species.now_ls)
|
||||
makeListRemoved(species.com_taxa_synonym)
|
||||
makeListRemoved(species.now_sau)
|
||||
|
||||
try {
|
||||
await writeHandler.applyListChanges('now_ls', species.now_ls, ['lid', 'species_id'])
|
||||
await writeHandler.applyListChanges('com_taxa_synonym', species.com_taxa_synonym, ['synonym_id', 'species_id'])
|
||||
await writeHandler.applyListChanges('now_sau', species.now_sau, ['suid'])
|
||||
await writeHandler.deleteObject('com_species', species, ['species_id'])
|
||||
await writeHandler.logUpdatesAndComplete(user.initials)
|
||||
} catch (e) {
|
||||
await writeHandler.end()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ export type AllowedTables =
|
||||
| 'now_tu_bound'
|
||||
| 'ref_ref'
|
||||
| 'now_tu_sequence'
|
||||
| 'now_sau'
|
||||
|
||||
export type PrimaryTables = 'now_loc' | 'com_species' | 'now_time_unit' | 'now_tu_bound'
|
||||
|
||||
|
||||
Vendored
+4
-4
@@ -25,7 +25,7 @@ type EditDataType<T> = T extends object
|
||||
// Changes all bigints to number type including in nested objects
|
||||
export type FixBigInt<T> = {
|
||||
[K in keyof T]: T[K] extends bigint | null
|
||||
? number
|
||||
? number | null
|
||||
: T[K] extends (infer U)[]
|
||||
? FixBigInt<U>[]
|
||||
: T[K] extends object
|
||||
@@ -35,7 +35,7 @@ export type FixBigInt<T> = {
|
||||
|
||||
export type SpeciesType = FixBigInt<Prisma.com_species>
|
||||
export type RowState = 'new' | 'removed' | 'cancelled' | 'clean'
|
||||
export type UpdateComment = { update_comment: string }
|
||||
|
||||
// Use this for fields that include array that has to be edited by EditableTable.
|
||||
// For example see LocalityDetails: museums field
|
||||
export type Editable<T> = T & { rowState?: RowState }
|
||||
@@ -69,7 +69,7 @@ export type LocalityDetailsType = Prisma.now_loc & {
|
||||
now_ss: SedimentaryStructure[]
|
||||
} & {
|
||||
now_coll_meth: CollectingMethod[]
|
||||
} & { now_lau: Array<LocalityUpdate> } & UpdateComment
|
||||
} & { now_lau: Array<LocalityUpdate> }
|
||||
export type UpdateLog = LogPrisma.log
|
||||
export type Locality = {
|
||||
lid: number
|
||||
@@ -85,7 +85,7 @@ export type Locality = {
|
||||
export type Sequence = Prisma.now_tu_sequence
|
||||
export type SequenceDetailsType = Prisma.now_tu_sequence
|
||||
|
||||
export type SpeciesDetailsType = SpeciesType & { now_ls: Array<SpeciesLocality> } & {
|
||||
export type SpeciesDetailsType = Prisma.com_species & { now_ls: Array<SpeciesLocality> } & {
|
||||
com_taxa_synonym: Array<SpeciesSynonym>
|
||||
} & { now_sau: Array<SpeciesUpdate> }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user