diff --git a/helpers/databaseInitializer.js b/helpers/databaseInitializer.js index 4020bed2..85fde884 100644 --- a/helpers/databaseInitializer.js +++ b/helpers/databaseInitializer.js @@ -11,7 +11,7 @@ const recordColumns = " recordCreate_userName varchar(30) not null," + export const initLotOccupancyDB = () => { const lotOccupancyDB = sqlite(databasePath); const row = lotOccupancyDB - .prepare("select name from sqlite_master where type = 'table' and name = 'Lots'") + .prepare("select name from sqlite_master where type = 'table' and name = 'LotOccupancies'") .get(); if (!row) { debugSQL("Creating " + databasePath); @@ -140,7 +140,7 @@ export const initLotOccupancyDB = () => { lotOccupancyDB.prepare("create table if not exists LotOccupancies (" + "lotOccupancyId integer not null primary key autoincrement," + " occupancyTypeId integer not null," + - " lotId integer not null," + + " lotId integer," + " occupancyStartDate integer not null check (occupancyStartDate > 0)," + " occupancyEndDate integer check (occupancyEndDate > 0)," + recordColumns + "," + @@ -167,6 +167,17 @@ export const initLotOccupancyDB = () => { " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," + " foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" + ") without rowid").run(); + lotOccupancyDB.prepare("create table if not exists LotOccupancyComments (" + + "lotOccupancyCommentId integer not null primary key autoincrement," + + " lotOccupancyId integer not null," + + " lotOccupancyCommentDate integer not null check (lotOccupancyCommentDate > 0)," + + " lotOccupancyCommentTime integer not null check (lotOccupancyCommentTime >= 0)," + + " lotOccupancyComment text not null," + + recordColumns + "," + + " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)" + + ")").run(); + lotOccupancyDB.prepare("create index if not exists idx_lotoccupancycomments_datetime" + + " on LotOccupancyComments (lotOccupancyId, lotOccupancyCommentDate, lotOccupancyCommentTime)").run(); lotOccupancyDB.prepare("create table if not exists Fees (" + "feeId integer not null primary key autoincrement," + " feeName varchar(100) not null," + diff --git a/helpers/databaseInitializer.ts b/helpers/databaseInitializer.ts index 109ad6d8..45cc33dd 100644 --- a/helpers/databaseInitializer.ts +++ b/helpers/databaseInitializer.ts @@ -1,6 +1,8 @@ import sqlite from "better-sqlite3"; -import { lotOccupancyDB as databasePath } from "../data/databasePaths.js"; +import { + lotOccupancyDB as databasePath +} from "../data/databasePaths.js"; import debug from "debug"; const debugSQL = debug("lot-occupancy-system:databaseInitializer"); @@ -18,7 +20,7 @@ export const initLotOccupancyDB = (): boolean => { const lotOccupancyDB = sqlite(databasePath); const row = lotOccupancyDB - .prepare("select name from sqlite_master where type = 'table' and name = 'Lots'") + .prepare("select name from sqlite_master where type = 'table' and name = 'LotOccupancies'") .get(); if (!row) { @@ -52,7 +54,7 @@ export const initLotOccupancyDB = (): boolean => { ")").run(); lotOccupancyDB.prepare("create index if not exists idx_lottypefields_ordernumber" + - " on LotTypeFields (lotTypeId, orderNumber, lotTypeField)").run(); + " on LotTypeFields (lotTypeId, orderNumber, lotTypeField)").run(); lotOccupancyDB.prepare("create table if not exists LotStatuses (" + "lotStatusId integer not null primary key autoincrement," + @@ -128,7 +130,7 @@ export const initLotOccupancyDB = (): boolean => { " on LotComments (lotId, lotCommentDate, lotCommentTime)").run(); // Occupancies - + lotOccupancyDB.prepare("create table if not exists Occupants (" + "occupantId integer not null primary key autoincrement," + " occupantName varchar(200) not null," + @@ -139,7 +141,7 @@ export const initLotOccupancyDB = (): boolean => { " occupantPostalCode varchar(7)," + " occupantPhoneNumber varchar(30)," + recordColumns + - ")").run(); + ")").run(); lotOccupancyDB.prepare("create table if not exists OccupancyTypes (" + "occupancyTypeId integer not null primary key autoincrement," + @@ -164,7 +166,7 @@ export const initLotOccupancyDB = (): boolean => { recordColumns + "," + " foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" + ")").run(); - + lotOccupancyDB.prepare("create index if not exists idx_occupancytypefields_ordernumber" + " on OccupancyTypeFields (occupancyTypeId, orderNumber, occupancyTypeField)").run(); @@ -177,11 +179,11 @@ export const initLotOccupancyDB = (): boolean => { lotOccupancyDB.prepare("create index if not exists idx_lotoccupanttypes_ordernumber" + " on LotOccupantTypes (orderNumber, lotOccupantType)").run(); - + lotOccupancyDB.prepare("create table if not exists LotOccupancies (" + "lotOccupancyId integer not null primary key autoincrement," + " occupancyTypeId integer not null," + - " lotId integer not null," + + " lotId integer," + " occupancyStartDate integer not null check (occupancyStartDate > 0)," + " occupancyEndDate integer check (occupancyEndDate > 0)," + recordColumns + "," + @@ -189,17 +191,17 @@ export const initLotOccupancyDB = (): boolean => { " foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" + ")").run(); - lotOccupancyDB.prepare("create table if not exists LotOccupancyOccupants (" + - "lotOccupancyId integer not null," + - " lotOccupantIndex integer not null," + - " occupantId integer not null," + - " lotOccupantTypeId integer not null," + - recordColumns + "," + - " primary key (lotOccupancyId, lotOccupantIndex)," + - " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," + - " foreign key (occupantId) references Occupants (occupantId)," + - " foreign key (lotOccupantTypeId) references LotOccupantTypes (lotOccupantTypeId)" + - ") without rowid").run(); + lotOccupancyDB.prepare("create table if not exists LotOccupancyOccupants (" + + "lotOccupancyId integer not null," + + " lotOccupantIndex integer not null," + + " occupantId integer not null," + + " lotOccupantTypeId integer not null," + + recordColumns + "," + + " primary key (lotOccupancyId, lotOccupantIndex)," + + " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," + + " foreign key (occupantId) references Occupants (occupantId)," + + " foreign key (lotOccupantTypeId) references LotOccupantTypes (lotOccupantTypeId)" + + ") without rowid").run(); lotOccupancyDB.prepare("create table if not exists LotOccupancyFields (" + "lotOccupancyId integer not null," + @@ -211,6 +213,19 @@ export const initLotOccupancyDB = (): boolean => { " foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" + ") without rowid").run(); + lotOccupancyDB.prepare("create table if not exists LotOccupancyComments (" + + "lotOccupancyCommentId integer not null primary key autoincrement," + + " lotOccupancyId integer not null," + + " lotOccupancyCommentDate integer not null check (lotOccupancyCommentDate > 0)," + + " lotOccupancyCommentTime integer not null check (lotOccupancyCommentTime >= 0)," + + " lotOccupancyComment text not null," + + recordColumns + "," + + " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)" + + ")").run(); + + lotOccupancyDB.prepare("create index if not exists idx_lotoccupancycomments_datetime" + + " on LotOccupancyComments (lotOccupancyId, lotOccupancyCommentDate, lotOccupancyCommentTime)").run(); + // Occupancy Fees and Transactions lotOccupancyDB.prepare("create table if not exists Fees (" + @@ -309,4 +324,4 @@ export const initLotOccupancyDB = (): boolean => { } return false; -}; +}; \ No newline at end of file diff --git a/helpers/lotOccupancyDB/addLotOccupancyComment.d.ts b/helpers/lotOccupancyDB/addLotOccupancyComment.d.ts new file mode 100644 index 00000000..e1b7a3d9 --- /dev/null +++ b/helpers/lotOccupancyDB/addLotOccupancyComment.d.ts @@ -0,0 +1,9 @@ +import type * as recordTypes from "../../types/recordTypes"; +interface AddLotOccupancyCommentForm { + lotOccupancyId: string | number; + lotOccupancyCommentDateString: string; + lotOccupancyCommentTimeString: string; + lotOccupancyComment: string; +} +export declare const addLotOccupancyComment: (commentForm: AddLotOccupancyCommentForm, requestSession: recordTypes.PartialSession) => number; +export default addLotOccupancyComment; diff --git a/helpers/lotOccupancyDB/addLotOccupancyComment.js b/helpers/lotOccupancyDB/addLotOccupancyComment.js new file mode 100644 index 00000000..9477e3f5 --- /dev/null +++ b/helpers/lotOccupancyDB/addLotOccupancyComment.js @@ -0,0 +1,17 @@ +import { dateStringToInteger, timeStringToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js"; +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; +export const addLotOccupancyComment = (commentForm, requestSession) => { + const database = sqlite(databasePath); + const rightNowMillis = Date.now(); + const result = database + .prepare("insert into LotOccupancyComments (" + + "lotOccupancyId, lotOccupancyCommentDate, lotOccupancyCommentTime, lotOccupancyComment," + + " recordCreate_userName, recordCreate_timeMillis," + + " recordUpdate_userName, recordUpdate_timeMillis)" + + " values (?, ?, ?, ?, ?, ?, ?, ?)") + .run(commentForm.lotOccupancyId, dateStringToInteger(commentForm.lotOccupancyCommentDateString), timeStringToInteger(commentForm.lotOccupancyCommentTimeString), commentForm.lotOccupancyComment, requestSession.user.userName, rightNowMillis, requestSession.user.userName, rightNowMillis); + database.close(); + return result.lastInsertRowid; +}; +export default addLotOccupancyComment; diff --git a/helpers/lotOccupancyDB/addLotOccupancyComment.ts b/helpers/lotOccupancyDB/addLotOccupancyComment.ts new file mode 100644 index 00000000..799db476 --- /dev/null +++ b/helpers/lotOccupancyDB/addLotOccupancyComment.ts @@ -0,0 +1,44 @@ +import { dateStringToInteger, timeStringToInteger } from "@cityssm/expressjs-server-js/dateTimeFns.js"; +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; + +import type * as recordTypes from "../../types/recordTypes"; + + +interface AddLotOccupancyCommentForm { + lotOccupancyId: string | number; + lotOccupancyCommentDateString: string; + lotOccupancyCommentTimeString: string; + lotOccupancyComment: string; +} + + +export const addLotOccupancyComment = + (commentForm: AddLotOccupancyCommentForm, requestSession: recordTypes.PartialSession): number => { + + const database = sqlite(databasePath); + + const rightNowMillis = Date.now(); + + const result = database + .prepare("insert into LotOccupancyComments (" + + "lotOccupancyId, lotOccupancyCommentDate, lotOccupancyCommentTime, lotOccupancyComment," + + " recordCreate_userName, recordCreate_timeMillis," + + " recordUpdate_userName, recordUpdate_timeMillis)" + + " values (?, ?, ?, ?, ?, ?, ?, ?)") + .run(commentForm.lotOccupancyId, + dateStringToInteger(commentForm.lotOccupancyCommentDateString), + timeStringToInteger(commentForm.lotOccupancyCommentTimeString), + commentForm.lotOccupancyComment, + requestSession.user.userName, + rightNowMillis, + requestSession.user.userName, + rightNowMillis); + + database.close(); + + return result.lastInsertRowid as number; + }; + + +export default addLotOccupancyComment; \ No newline at end of file