add lot occupancy comment

deepsource-autofix-76c6eb20
Dan Gowans 2022-07-22 10:53:33 -04:00
parent 2836831c54
commit 727932bda9
5 changed files with 118 additions and 22 deletions

View File

@ -11,7 +11,7 @@ const recordColumns = " recordCreate_userName varchar(30) not null," +
export const initLotOccupancyDB = () => { export const initLotOccupancyDB = () => {
const lotOccupancyDB = sqlite(databasePath); const lotOccupancyDB = sqlite(databasePath);
const row = lotOccupancyDB 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(); .get();
if (!row) { if (!row) {
debugSQL("Creating " + databasePath); debugSQL("Creating " + databasePath);
@ -140,7 +140,7 @@ export const initLotOccupancyDB = () => {
lotOccupancyDB.prepare("create table if not exists LotOccupancies (" + lotOccupancyDB.prepare("create table if not exists LotOccupancies (" +
"lotOccupancyId integer not null primary key autoincrement," + "lotOccupancyId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," + " occupancyTypeId integer not null," +
" lotId integer not null," + " lotId integer," +
" occupancyStartDate integer not null check (occupancyStartDate > 0)," + " occupancyStartDate integer not null check (occupancyStartDate > 0)," +
" occupancyEndDate integer check (occupancyEndDate > 0)," + " occupancyEndDate integer check (occupancyEndDate > 0)," +
recordColumns + "," + recordColumns + "," +
@ -167,6 +167,17 @@ export const initLotOccupancyDB = () => {
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," + " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" + " foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" +
") without rowid").run(); ") 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 (" + lotOccupancyDB.prepare("create table if not exists Fees (" +
"feeId integer not null primary key autoincrement," + "feeId integer not null primary key autoincrement," +
" feeName varchar(100) not null," + " feeName varchar(100) not null," +

View File

@ -1,6 +1,8 @@
import sqlite from "better-sqlite3"; 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"; import debug from "debug";
const debugSQL = debug("lot-occupancy-system:databaseInitializer"); const debugSQL = debug("lot-occupancy-system:databaseInitializer");
@ -18,7 +20,7 @@ export const initLotOccupancyDB = (): boolean => {
const lotOccupancyDB = sqlite(databasePath); const lotOccupancyDB = sqlite(databasePath);
const row = lotOccupancyDB 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(); .get();
if (!row) { if (!row) {
@ -52,7 +54,7 @@ export const initLotOccupancyDB = (): boolean => {
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypefields_ordernumber" + 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 (" + lotOccupancyDB.prepare("create table if not exists LotStatuses (" +
"lotStatusId integer not null primary key autoincrement," + "lotStatusId integer not null primary key autoincrement," +
@ -128,7 +130,7 @@ export const initLotOccupancyDB = (): boolean => {
" on LotComments (lotId, lotCommentDate, lotCommentTime)").run(); " on LotComments (lotId, lotCommentDate, lotCommentTime)").run();
// Occupancies // Occupancies
lotOccupancyDB.prepare("create table if not exists Occupants (" + lotOccupancyDB.prepare("create table if not exists Occupants (" +
"occupantId integer not null primary key autoincrement," + "occupantId integer not null primary key autoincrement," +
" occupantName varchar(200) not null," + " occupantName varchar(200) not null," +
@ -139,7 +141,7 @@ export const initLotOccupancyDB = (): boolean => {
" occupantPostalCode varchar(7)," + " occupantPostalCode varchar(7)," +
" occupantPhoneNumber varchar(30)," + " occupantPhoneNumber varchar(30)," +
recordColumns + recordColumns +
")").run(); ")").run();
lotOccupancyDB.prepare("create table if not exists OccupancyTypes (" + lotOccupancyDB.prepare("create table if not exists OccupancyTypes (" +
"occupancyTypeId integer not null primary key autoincrement," + "occupancyTypeId integer not null primary key autoincrement," +
@ -164,7 +166,7 @@ export const initLotOccupancyDB = (): boolean => {
recordColumns + "," + recordColumns + "," +
" foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" + " foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_occupancytypefields_ordernumber" + lotOccupancyDB.prepare("create index if not exists idx_occupancytypefields_ordernumber" +
" on OccupancyTypeFields (occupancyTypeId, orderNumber, occupancyTypeField)").run(); " on OccupancyTypeFields (occupancyTypeId, orderNumber, occupancyTypeField)").run();
@ -177,11 +179,11 @@ export const initLotOccupancyDB = (): boolean => {
lotOccupancyDB.prepare("create index if not exists idx_lotoccupanttypes_ordernumber" + lotOccupancyDB.prepare("create index if not exists idx_lotoccupanttypes_ordernumber" +
" on LotOccupantTypes (orderNumber, lotOccupantType)").run(); " on LotOccupantTypes (orderNumber, lotOccupantType)").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancies (" + lotOccupancyDB.prepare("create table if not exists LotOccupancies (" +
"lotOccupancyId integer not null primary key autoincrement," + "lotOccupancyId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," + " occupancyTypeId integer not null," +
" lotId integer not null," + " lotId integer," +
" occupancyStartDate integer not null check (occupancyStartDate > 0)," + " occupancyStartDate integer not null check (occupancyStartDate > 0)," +
" occupancyEndDate integer check (occupancyEndDate > 0)," + " occupancyEndDate integer check (occupancyEndDate > 0)," +
recordColumns + "," + recordColumns + "," +
@ -189,17 +191,17 @@ export const initLotOccupancyDB = (): boolean => {
" foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" + " foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyOccupants (" + lotOccupancyDB.prepare("create table if not exists LotOccupancyOccupants (" +
"lotOccupancyId integer not null," + "lotOccupancyId integer not null," +
" lotOccupantIndex integer not null," + " lotOccupantIndex integer not null," +
" occupantId integer not null," + " occupantId integer not null," +
" lotOccupantTypeId integer not null," + " lotOccupantTypeId integer not null," +
recordColumns + "," + recordColumns + "," +
" primary key (lotOccupancyId, lotOccupantIndex)," + " primary key (lotOccupancyId, lotOccupantIndex)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," + " foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (occupantId) references Occupants (occupantId)," + " foreign key (occupantId) references Occupants (occupantId)," +
" foreign key (lotOccupantTypeId) references LotOccupantTypes (lotOccupantTypeId)" + " foreign key (lotOccupantTypeId) references LotOccupantTypes (lotOccupantTypeId)" +
") without rowid").run(); ") without rowid").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyFields (" + lotOccupancyDB.prepare("create table if not exists LotOccupancyFields (" +
"lotOccupancyId integer not null," + "lotOccupancyId integer not null," +
@ -211,6 +213,19 @@ export const initLotOccupancyDB = (): boolean => {
" foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" + " foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" +
") without rowid").run(); ") 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 // Occupancy Fees and Transactions
lotOccupancyDB.prepare("create table if not exists Fees (" + lotOccupancyDB.prepare("create table if not exists Fees (" +
@ -309,4 +324,4 @@ export const initLotOccupancyDB = (): boolean => {
} }
return false; return false;
}; };

View File

@ -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;

View File

@ -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;

View File

@ -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;