rebuild burial site names on cemetery key change
parent
1f19359195
commit
4d3ba54d50
|
|
@ -0,0 +1,2 @@
|
|||
import type { PoolConnection } from 'better-sqlite-pool';
|
||||
export default function rebuildBurialSiteNames(cemeteryId: number | string, user: User, connectedDatabase?: PoolConnection): Promise<number>;
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
import { buildBurialSiteName } from '../helpers/burialSites.helpers.js';
|
||||
import getBurialSites from './getBurialSites.js';
|
||||
import getCemetery from './getCemetery.js';
|
||||
import { acquireConnection } from './pool.js';
|
||||
export default async function rebuildBurialSiteNames(cemeteryId, user, connectedDatabase) {
|
||||
const database = connectedDatabase ?? (await acquireConnection());
|
||||
/*
|
||||
* Get the cemetery key
|
||||
*/
|
||||
const cemetery = await getCemetery(cemeteryId, database);
|
||||
if (cemetery === undefined) {
|
||||
if (connectedDatabase === undefined) {
|
||||
database.release();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* Get the burial sites
|
||||
*/
|
||||
const burialSites = await getBurialSites({
|
||||
cemeteryId
|
||||
}, {
|
||||
limit: -1,
|
||||
offset: 0
|
||||
}, database);
|
||||
let updateCount = 0;
|
||||
for (const burialSite of burialSites.burialSites) {
|
||||
const burialSiteName = buildBurialSiteName(cemetery.cemeteryKey, burialSite);
|
||||
if (burialSiteName !== burialSite.burialSiteName) {
|
||||
const result = database
|
||||
.prepare(`update BurialSites
|
||||
set burialSiteName = ?,
|
||||
recordUpdate_userName = ?,
|
||||
recordUpdate_timeMillis = ?
|
||||
where burialSiteId = ?
|
||||
and recordDelete_timeMillis is null`)
|
||||
.run(burialSiteName, user.userName, Date.now(), burialSite.burialSiteId);
|
||||
updateCount += result.changes;
|
||||
}
|
||||
}
|
||||
if (connectedDatabase === undefined) {
|
||||
database.release();
|
||||
}
|
||||
return updateCount;
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
import type { PoolConnection } from 'better-sqlite-pool'
|
||||
|
||||
import { buildBurialSiteName } from '../helpers/burialSites.helpers.js'
|
||||
|
||||
import getBurialSites from './getBurialSites.js'
|
||||
import getCemetery from './getCemetery.js'
|
||||
import { acquireConnection } from './pool.js'
|
||||
|
||||
export default async function rebuildBurialSiteNames(
|
||||
cemeteryId: number | string,
|
||||
user: User,
|
||||
connectedDatabase?: PoolConnection
|
||||
): Promise<number> {
|
||||
const database = connectedDatabase ?? (await acquireConnection())
|
||||
|
||||
/*
|
||||
* Get the cemetery key
|
||||
*/
|
||||
|
||||
const cemetery = await getCemetery(cemeteryId, database)
|
||||
|
||||
if (cemetery === undefined) {
|
||||
if (connectedDatabase === undefined) {
|
||||
database.release()
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the burial sites
|
||||
*/
|
||||
|
||||
const burialSites = await getBurialSites(
|
||||
{
|
||||
cemeteryId
|
||||
},
|
||||
{
|
||||
limit: -1,
|
||||
offset: 0
|
||||
},
|
||||
database
|
||||
)
|
||||
|
||||
let updateCount = 0
|
||||
|
||||
for (const burialSite of burialSites.burialSites) {
|
||||
const burialSiteName = buildBurialSiteName(cemetery.cemeteryKey, burialSite)
|
||||
|
||||
if (burialSiteName !== burialSite.burialSiteName) {
|
||||
const result = database
|
||||
.prepare(
|
||||
`update BurialSites
|
||||
set burialSiteName = ?,
|
||||
recordUpdate_userName = ?,
|
||||
recordUpdate_timeMillis = ?
|
||||
where burialSiteId = ?
|
||||
and recordDelete_timeMillis is null`
|
||||
)
|
||||
.run(burialSiteName, user.userName, Date.now(), burialSite.burialSiteId)
|
||||
|
||||
updateCount += result.changes
|
||||
}
|
||||
}
|
||||
|
||||
if (connectedDatabase === undefined) {
|
||||
database.release()
|
||||
}
|
||||
return updateCount
|
||||
}
|
||||
|
|
@ -1,16 +1,19 @@
|
|||
export interface UpdateCemeteryForm {
|
||||
cemeteryId: string;
|
||||
cemeteryName: string;
|
||||
cemeteryKey: string;
|
||||
cemeteryDescription: string;
|
||||
cemeterySvg: string;
|
||||
cemeteryLatitude: string;
|
||||
cemeteryLongitude: string;
|
||||
cemeteryKey: string;
|
||||
cemeteryName: string;
|
||||
cemeteryAddress1: string;
|
||||
cemeteryAddress2: string;
|
||||
cemeteryCity: string;
|
||||
cemeteryProvince: string;
|
||||
cemeteryPostalCode: string;
|
||||
cemeteryProvince: string;
|
||||
cemeteryPhoneNumber: string;
|
||||
cemeteryLatitude: string;
|
||||
cemeteryLongitude: string;
|
||||
cemeterySvg: string;
|
||||
}
|
||||
export default function updateCemetery(updateForm: UpdateCemeteryForm, user: User): Promise<boolean>;
|
||||
export default function updateCemetery(updateForm: UpdateCemeteryForm, user: User): Promise<{
|
||||
doRebuildBurialSiteNames: boolean;
|
||||
success: boolean;
|
||||
}>;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
import { buildBurialSiteName } from '../helpers/burialSites.helpers.js';
|
||||
import { getConfigProperty } from '../helpers/config.helpers.js';
|
||||
import getBurialSites from './getBurialSites.js';
|
||||
import { acquireConnection } from './pool.js';
|
||||
export default async function updateCemetery(updateForm, user) {
|
||||
const database = await acquireConnection();
|
||||
|
|
@ -24,6 +27,26 @@ export default async function updateCemetery(updateForm, user) {
|
|||
: updateForm.cemeteryLatitude, updateForm.cemeteryLongitude === ''
|
||||
? undefined
|
||||
: updateForm.cemeteryLongitude, updateForm.cemeteryAddress1, updateForm.cemeteryAddress2, updateForm.cemeteryCity, updateForm.cemeteryProvince, updateForm.cemeteryPostalCode, updateForm.cemeteryPhoneNumber, user.userName, Date.now(), updateForm.cemeteryId);
|
||||
database.release();
|
||||
return result.changes > 0;
|
||||
/*
|
||||
* Check if burial site names need to be updated
|
||||
*/
|
||||
let doRebuildBurialSiteNames = false;
|
||||
if (getConfigProperty('settings.burialSites.burialSiteNameSegments.includeCemeteryKey')) {
|
||||
const burialSites = await getBurialSites({ cemeteryId: updateForm.cemeteryId }, { limit: 1, offset: 0 }, database);
|
||||
if (burialSites.count > 0 &&
|
||||
buildBurialSiteName(updateForm.cemeteryKey, {
|
||||
burialSiteNameSegment1: burialSites.burialSites[0].burialSiteNameSegment1,
|
||||
burialSiteNameSegment2: burialSites.burialSites[0].burialSiteNameSegment2,
|
||||
burialSiteNameSegment3: burialSites.burialSites[0].burialSiteNameSegment3,
|
||||
burialSiteNameSegment4: burialSites.burialSites[0].burialSiteNameSegment4,
|
||||
burialSiteNameSegment5: burialSites.burialSites[0].burialSiteNameSegment5
|
||||
}) !== burialSites.burialSites[0].burialSiteName) {
|
||||
doRebuildBurialSiteNames = true;
|
||||
}
|
||||
}
|
||||
database.release();
|
||||
return {
|
||||
doRebuildBurialSiteNames,
|
||||
success: result.changes > 0
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,33 @@
|
|||
import { buildBurialSiteName } from '../helpers/burialSites.helpers.js'
|
||||
import { getConfigProperty } from '../helpers/config.helpers.js'
|
||||
|
||||
import getBurialSites from './getBurialSites.js'
|
||||
import { acquireConnection } from './pool.js'
|
||||
|
||||
export interface UpdateCemeteryForm {
|
||||
cemeteryId: string
|
||||
cemeteryName: string
|
||||
cemeteryKey: string
|
||||
|
||||
cemeteryDescription: string
|
||||
cemeterySvg: string
|
||||
cemeteryLatitude: string
|
||||
cemeteryLongitude: string
|
||||
cemeteryKey: string
|
||||
cemeteryName: string
|
||||
|
||||
cemeteryAddress1: string
|
||||
cemeteryAddress2: string
|
||||
cemeteryCity: string
|
||||
cemeteryProvince: string
|
||||
cemeteryPostalCode: string
|
||||
cemeteryProvince: string
|
||||
|
||||
cemeteryPhoneNumber: string
|
||||
|
||||
cemeteryLatitude: string
|
||||
cemeteryLongitude: string
|
||||
cemeterySvg: string
|
||||
}
|
||||
|
||||
export default async function updateCemetery(
|
||||
updateForm: UpdateCemeteryForm,
|
||||
user: User
|
||||
): Promise<boolean> {
|
||||
): Promise<{ doRebuildBurialSiteNames: boolean; success: boolean; }> {
|
||||
const database = await acquireConnection()
|
||||
|
||||
const result = database
|
||||
|
|
@ -64,7 +72,46 @@ export default async function updateCemetery(
|
|||
updateForm.cemeteryId
|
||||
)
|
||||
|
||||
/*
|
||||
* Check if burial site names need to be updated
|
||||
*/
|
||||
|
||||
let doRebuildBurialSiteNames = false
|
||||
|
||||
if (
|
||||
getConfigProperty(
|
||||
'settings.burialSites.burialSiteNameSegments.includeCemeteryKey'
|
||||
)
|
||||
) {
|
||||
const burialSites = await getBurialSites(
|
||||
{ cemeteryId: updateForm.cemeteryId },
|
||||
{ limit: 1, offset: 0 },
|
||||
database
|
||||
)
|
||||
|
||||
if (
|
||||
burialSites.count > 0 &&
|
||||
buildBurialSiteName(updateForm.cemeteryKey, {
|
||||
burialSiteNameSegment1:
|
||||
burialSites.burialSites[0].burialSiteNameSegment1,
|
||||
burialSiteNameSegment2:
|
||||
burialSites.burialSites[0].burialSiteNameSegment2,
|
||||
burialSiteNameSegment3:
|
||||
burialSites.burialSites[0].burialSiteNameSegment3,
|
||||
burialSiteNameSegment4:
|
||||
burialSites.burialSites[0].burialSiteNameSegment4,
|
||||
burialSiteNameSegment5:
|
||||
burialSites.burialSites[0].burialSiteNameSegment5
|
||||
}) !== burialSites.burialSites[0].burialSiteName
|
||||
) {
|
||||
doRebuildBurialSiteNames = true
|
||||
}
|
||||
}
|
||||
|
||||
database.release()
|
||||
|
||||
return result.changes > 0
|
||||
return {
|
||||
doRebuildBurialSiteNames,
|
||||
success: result.changes > 0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,17 @@
|
|||
import rebuildBurialSiteNames from '../../database/rebuildBurialSiteNames.js';
|
||||
import updateCemetery from '../../database/updateCemetery.js';
|
||||
import { clearNextPreviousBurialSiteIdCache } from '../../helpers/burialSites.helpers.js';
|
||||
export default async function handler(request, response) {
|
||||
const success = await updateCemetery(request.body, request.session.user);
|
||||
const result = await updateCemetery(request.body, request.session.user);
|
||||
response.json({
|
||||
success,
|
||||
cemeteryId: request.body.cemeteryId
|
||||
success: result.success,
|
||||
cemeteryId: request.body.cemeteryId,
|
||||
doRebuildBurialSiteNames: result.doRebuildBurialSiteNames
|
||||
});
|
||||
if (result.doRebuildBurialSiteNames) {
|
||||
response.on('finish', () => {
|
||||
void rebuildBurialSiteNames(request.body.cemeteryId, request.session.user);
|
||||
clearNextPreviousBurialSiteIdCache();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,35 @@
|
|||
import type { Request, Response } from 'express'
|
||||
|
||||
import rebuildBurialSiteNames from '../../database/rebuildBurialSiteNames.js'
|
||||
import updateCemetery, {
|
||||
type UpdateCemeteryForm
|
||||
} from '../../database/updateCemetery.js'
|
||||
import { clearNextPreviousBurialSiteIdCache } from '../../helpers/burialSites.helpers.js'
|
||||
|
||||
export default async function handler(
|
||||
request: Request<unknown, unknown, UpdateCemeteryForm>,
|
||||
response: Response
|
||||
): Promise<void> {
|
||||
const success = await updateCemetery(
|
||||
const result = await updateCemetery(
|
||||
request.body,
|
||||
request.session.user as User
|
||||
)
|
||||
|
||||
response.json({
|
||||
success,
|
||||
cemeteryId: request.body.cemeteryId
|
||||
success: result.success,
|
||||
|
||||
cemeteryId: request.body.cemeteryId,
|
||||
doRebuildBurialSiteNames: result.doRebuildBurialSiteNames
|
||||
})
|
||||
|
||||
if (result.doRebuildBurialSiteNames) {
|
||||
response.on('finish', () => {
|
||||
void rebuildBurialSiteNames(
|
||||
request.body.cemeteryId,
|
||||
request.session.user as User
|
||||
)
|
||||
|
||||
clearNextPreviousBurialSiteIdCache()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
if (isCreate) {
|
||||
globalThis.location.href = sunrise.getCemeteryURL(responseJSON.cemeteryId, true);
|
||||
}
|
||||
else {
|
||||
if (responseJSON.doRebuildBurialSiteNames ?? false) {
|
||||
bulmaJS.alert({
|
||||
message: `<strong>Cemetery Updated Successfully</strong><br />
|
||||
Note that rebuilding burial site names may take a few minutes.`,
|
||||
messageIsHtml: true,
|
||||
contextualColorName: 'warning'
|
||||
});
|
||||
}
|
||||
else {
|
||||
bulmaJS.alert({
|
||||
message: 'Cemetery Updated Successfully',
|
||||
|
|
@ -51,6 +60,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
bulmaJS.alert({
|
||||
title: 'Error Updating Cemetery',
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ declare const exports: Record<string, unknown>
|
|||
const responseJSON = rawResponseJSON as {
|
||||
success: boolean
|
||||
cemeteryId?: number
|
||||
doRebuildBurialSiteNames?: boolean
|
||||
errorMessage?: string
|
||||
}
|
||||
|
||||
|
|
@ -83,11 +84,21 @@ declare const exports: Record<string, unknown>
|
|||
true
|
||||
)
|
||||
} else {
|
||||
if (responseJSON.doRebuildBurialSiteNames ?? false) {
|
||||
bulmaJS.alert({
|
||||
message: `<strong>Cemetery Updated Successfully</strong><br />
|
||||
Note that rebuilding burial site names may take a few minutes.`,
|
||||
messageIsHtml: true,
|
||||
contextualColorName: 'warning'
|
||||
})
|
||||
} else {
|
||||
|
||||
bulmaJS.alert({
|
||||
message: 'Cemetery Updated Successfully',
|
||||
contextualColorName: 'success'
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bulmaJS.alert({
|
||||
title: 'Error Updating Cemetery',
|
||||
|
|
|
|||
Loading…
Reference in New Issue