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 {
|
export interface UpdateCemeteryForm {
|
||||||
cemeteryId: string;
|
cemeteryId: string;
|
||||||
cemeteryName: string;
|
|
||||||
cemeteryKey: string;
|
|
||||||
cemeteryDescription: string;
|
cemeteryDescription: string;
|
||||||
cemeterySvg: string;
|
cemeteryKey: string;
|
||||||
cemeteryLatitude: string;
|
cemeteryName: string;
|
||||||
cemeteryLongitude: string;
|
|
||||||
cemeteryAddress1: string;
|
cemeteryAddress1: string;
|
||||||
cemeteryAddress2: string;
|
cemeteryAddress2: string;
|
||||||
cemeteryCity: string;
|
cemeteryCity: string;
|
||||||
cemeteryProvince: string;
|
|
||||||
cemeteryPostalCode: string;
|
cemeteryPostalCode: string;
|
||||||
|
cemeteryProvince: string;
|
||||||
cemeteryPhoneNumber: 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';
|
import { acquireConnection } from './pool.js';
|
||||||
export default async function updateCemetery(updateForm, user) {
|
export default async function updateCemetery(updateForm, user) {
|
||||||
const database = await acquireConnection();
|
const database = await acquireConnection();
|
||||||
|
|
@ -24,6 +27,26 @@ export default async function updateCemetery(updateForm, user) {
|
||||||
: updateForm.cemeteryLatitude, updateForm.cemeteryLongitude === ''
|
: updateForm.cemeteryLatitude, updateForm.cemeteryLongitude === ''
|
||||||
? undefined
|
? undefined
|
||||||
: updateForm.cemeteryLongitude, updateForm.cemeteryAddress1, updateForm.cemeteryAddress2, updateForm.cemeteryCity, updateForm.cemeteryProvince, updateForm.cemeteryPostalCode, updateForm.cemeteryPhoneNumber, user.userName, Date.now(), updateForm.cemeteryId);
|
: updateForm.cemeteryLongitude, updateForm.cemeteryAddress1, updateForm.cemeteryAddress2, updateForm.cemeteryCity, updateForm.cemeteryProvince, updateForm.cemeteryPostalCode, updateForm.cemeteryPhoneNumber, user.userName, Date.now(), 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();
|
database.release();
|
||||||
return result.changes > 0;
|
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'
|
import { acquireConnection } from './pool.js'
|
||||||
|
|
||||||
export interface UpdateCemeteryForm {
|
export interface UpdateCemeteryForm {
|
||||||
cemeteryId: string
|
cemeteryId: string
|
||||||
cemeteryName: string
|
|
||||||
cemeteryKey: string
|
|
||||||
cemeteryDescription: string
|
cemeteryDescription: string
|
||||||
cemeterySvg: string
|
cemeteryKey: string
|
||||||
cemeteryLatitude: string
|
cemeteryName: string
|
||||||
cemeteryLongitude: string
|
|
||||||
cemeteryAddress1: string
|
cemeteryAddress1: string
|
||||||
cemeteryAddress2: string
|
cemeteryAddress2: string
|
||||||
cemeteryCity: string
|
cemeteryCity: string
|
||||||
cemeteryProvince: string
|
|
||||||
cemeteryPostalCode: string
|
cemeteryPostalCode: string
|
||||||
|
cemeteryProvince: string
|
||||||
|
|
||||||
cemeteryPhoneNumber: string
|
cemeteryPhoneNumber: string
|
||||||
|
|
||||||
|
cemeteryLatitude: string
|
||||||
|
cemeteryLongitude: string
|
||||||
|
cemeterySvg: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function updateCemetery(
|
export default async function updateCemetery(
|
||||||
updateForm: UpdateCemeteryForm,
|
updateForm: UpdateCemeteryForm,
|
||||||
user: User
|
user: User
|
||||||
): Promise<boolean> {
|
): Promise<{ doRebuildBurialSiteNames: boolean; success: boolean; }> {
|
||||||
const database = await acquireConnection()
|
const database = await acquireConnection()
|
||||||
|
|
||||||
const result = database
|
const result = database
|
||||||
|
|
@ -64,7 +72,46 @@ export default async function updateCemetery(
|
||||||
updateForm.cemeteryId
|
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()
|
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 updateCemetery from '../../database/updateCemetery.js';
|
||||||
|
import { clearNextPreviousBurialSiteIdCache } from '../../helpers/burialSites.helpers.js';
|
||||||
export default async function handler(request, response) {
|
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({
|
response.json({
|
||||||
success,
|
success: result.success,
|
||||||
cemeteryId: request.body.cemeteryId
|
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 type { Request, Response } from 'express'
|
||||||
|
|
||||||
|
import rebuildBurialSiteNames from '../../database/rebuildBurialSiteNames.js'
|
||||||
import updateCemetery, {
|
import updateCemetery, {
|
||||||
type UpdateCemeteryForm
|
type UpdateCemeteryForm
|
||||||
} from '../../database/updateCemetery.js'
|
} from '../../database/updateCemetery.js'
|
||||||
|
import { clearNextPreviousBurialSiteIdCache } from '../../helpers/burialSites.helpers.js'
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
request: Request<unknown, unknown, UpdateCemeteryForm>,
|
request: Request<unknown, unknown, UpdateCemeteryForm>,
|
||||||
response: Response
|
response: Response
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const success = await updateCemetery(
|
const result = await updateCemetery(
|
||||||
request.body,
|
request.body,
|
||||||
request.session.user as User
|
request.session.user as User
|
||||||
)
|
)
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
success,
|
success: result.success,
|
||||||
cemeteryId: request.body.cemeteryId
|
|
||||||
|
cemeteryId: request.body.cemeteryId,
|
||||||
|
doRebuildBurialSiteNames: result.doRebuildBurialSiteNames
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (result.doRebuildBurialSiteNames) {
|
||||||
|
response.on('finish', () => {
|
||||||
|
void rebuildBurialSiteNames(
|
||||||
|
request.body.cemeteryId,
|
||||||
|
request.session.user as User
|
||||||
|
)
|
||||||
|
|
||||||
|
clearNextPreviousBurialSiteIdCache()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
globalThis.location.href = sunrise.getCemeteryURL(responseJSON.cemeteryId, true);
|
globalThis.location.href = sunrise.getCemeteryURL(responseJSON.cemeteryId, true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bulmaJS.alert({
|
if (responseJSON.doRebuildBurialSiteNames ?? false) {
|
||||||
message: 'Cemetery Updated Successfully',
|
bulmaJS.alert({
|
||||||
contextualColorName: 'success'
|
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 {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,7 @@ declare const exports: Record<string, unknown>
|
||||||
const responseJSON = rawResponseJSON as {
|
const responseJSON = rawResponseJSON as {
|
||||||
success: boolean
|
success: boolean
|
||||||
cemeteryId?: number
|
cemeteryId?: number
|
||||||
|
doRebuildBurialSiteNames?: boolean
|
||||||
errorMessage?: string
|
errorMessage?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,10 +84,20 @@ declare const exports: Record<string, unknown>
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
bulmaJS.alert({
|
if (responseJSON.doRebuildBurialSiteNames ?? false) {
|
||||||
message: 'Cemetery Updated Successfully',
|
bulmaJS.alert({
|
||||||
contextualColorName: 'success'
|
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 {
|
} else {
|
||||||
bulmaJS.alert({
|
bulmaJS.alert({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue