Skip to content

Commit

Permalink
manage asset group members
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Aug 17, 2023
1 parent 61020ac commit 435ee92
Show file tree
Hide file tree
Showing 38 changed files with 1,210 additions and 613 deletions.
2 changes: 1 addition & 1 deletion database/addAssetGroup.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { AssetGroup } from '../types/recordTypes.js';
export declare function addAssetGroup(group: AssetGroup, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addAssetGroup(group: Partial<AssetGroup>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addAssetGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { AssetGroup } from '../types/recordTypes.js'

export function addAssetGroup(
group: AssetGroup,
group: Partial<AssetGroup>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
1 change: 1 addition & 0 deletions database/addAssetGroupMember.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function addAssetGroupMember(groupId: number | string, assetId: number | string, sessionUser: EmileUser): boolean;
27 changes: 27 additions & 0 deletions database/addAssetGroupMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
export function addAssetGroupMember(groupId, assetId, sessionUser) {
const emileDB = sqlite(databasePath);
const rightNowMillis = Date.now();
let result = emileDB
.prepare(`update AssetGroupMembers
set recordDelete_userName = null,
recordDelete_timeMillis = null,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordDelete_timeMillis is not null
and groupId = ?
and assetId = ?`)
.run(sessionUser.userName, rightNowMillis, groupId, assetId);
if (result.changes === 0) {
result = emileDB
.prepare(`insert into AssetGroupMembers (
groupId, assetId,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?)`)
.run(groupId, assetId, sessionUser.userName, rightNowMillis, sessionUser.userName, rightNowMillis);
}
emileDB.close();
return result.changes > 0;
}
49 changes: 49 additions & 0 deletions database/addAssetGroupMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sqlite from 'better-sqlite3'

import { databasePath } from '../helpers/functions.database.js'

export function addAssetGroupMember(
groupId: number | string,
assetId: number | string,
sessionUser: EmileUser
): boolean {
const emileDB = sqlite(databasePath)

const rightNowMillis = Date.now()

let result = emileDB
.prepare(
`update AssetGroupMembers
set recordDelete_userName = null,
recordDelete_timeMillis = null,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordDelete_timeMillis is not null
and groupId = ?
and assetId = ?`
)
.run(sessionUser.userName, rightNowMillis, groupId, assetId)

if (result.changes === 0) {
result = emileDB
.prepare(
`insert into AssetGroupMembers (
groupId, assetId,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?)`
)
.run(
groupId,
assetId,
sessionUser.userName,
rightNowMillis,
sessionUser.userName,
rightNowMillis
)
}

emileDB.close()

return result.changes > 0
}
2 changes: 1 addition & 1 deletion database/addEnergyAccumulationBehaviour.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyAccumulationBehaviour } from '../types/recordTypes.js';
export declare function addEnergyAccumulationBehaviour(accumulationBehaviour: EnergyAccumulationBehaviour, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyAccumulationBehaviour(accumulationBehaviour: Partial<EnergyAccumulationBehaviour>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyAccumulationBehaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyAccumulationBehaviour } from '../types/recordTypes.js'

export function addEnergyAccumulationBehaviour(
accumulationBehaviour: EnergyAccumulationBehaviour,
accumulationBehaviour: Partial<EnergyAccumulationBehaviour>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
2 changes: 1 addition & 1 deletion database/addEnergyCommodity.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyCommodity } from '../types/recordTypes.js';
export declare function addEnergyCommodity(commodity: EnergyCommodity, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyCommodity(commodity: Partial<EnergyCommodity>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyCommodity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyCommodity } from '../types/recordTypes.js'

export function addEnergyCommodity(
commodity: EnergyCommodity,
commodity: Partial<EnergyCommodity>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
2 changes: 1 addition & 1 deletion database/addEnergyDataType.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyDataType } from '../types/recordTypes.js';
export declare function addEnergyDataType(energyDataType: EnergyDataType, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyDataType(energyDataType: Partial<EnergyDataType>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyDataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyDataType } from '../types/recordTypes.js'

export function addEnergyDataType(
energyDataType: EnergyDataType,
energyDataType: Partial<EnergyDataType>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
2 changes: 1 addition & 1 deletion database/addEnergyReadingType.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyReadingType } from '../types/recordTypes.js';
export declare function addEnergyReadingType(readingType: EnergyReadingType, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyReadingType(readingType: Partial<EnergyReadingType>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyReadingType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyReadingType } from '../types/recordTypes.js'

export function addEnergyReadingType(
readingType: EnergyReadingType,
readingType: Partial<EnergyReadingType>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
2 changes: 1 addition & 1 deletion database/addEnergyServiceCategory.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyServiceCategory } from '../types/recordTypes.js';
export declare function addEnergyServiceCategory(serviceCategory: EnergyServiceCategory, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyServiceCategory(serviceCategory: Partial<EnergyServiceCategory>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyServiceCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyServiceCategory } from '../types/recordTypes.js'

export function addEnergyServiceCategory(
serviceCategory: EnergyServiceCategory,
serviceCategory: Partial<EnergyServiceCategory>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
2 changes: 1 addition & 1 deletion database/addEnergyUnit.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type { EnergyUnit } from '../types/recordTypes.js';
export declare function addEnergyUnit(unit: EnergyUnit, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
export declare function addEnergyUnit(unit: Partial<EnergyUnit>, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): number;
2 changes: 1 addition & 1 deletion database/addEnergyUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { databasePath } from '../helpers/functions.database.js'
import type { EnergyUnit } from '../types/recordTypes.js'

export function addEnergyUnit(
unit: EnergyUnit,
unit: Partial<EnergyUnit>,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): number {
Expand Down
1 change: 1 addition & 0 deletions database/deleteAssetGroupMember.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function deleteAssetGroupMember(groupId: number | string, assetId: number | string, sessionUser: EmileUser): boolean;
15 changes: 15 additions & 0 deletions database/deleteAssetGroupMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
export function deleteAssetGroupMember(groupId, assetId, sessionUser) {
const emileDB = sqlite(databasePath);
const result = emileDB
.prepare(`update AssetGroupMembers
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and groupId = ?
and assetId = ?`)
.run(sessionUser.userName, Date.now(), groupId, assetId);
emileDB.close();
return result.changes > 0;
}
26 changes: 26 additions & 0 deletions database/deleteAssetGroupMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sqlite from 'better-sqlite3'

import { databasePath } from '../helpers/functions.database.js'

export function deleteAssetGroupMember(
groupId: number | string,
assetId: number | string,
sessionUser: EmileUser
): boolean {
const emileDB = sqlite(databasePath)

const result = emileDB
.prepare(
`update AssetGroupMembers
set recordDelete_userName = ?,
recordDelete_timeMillis = ?
where recordDelete_timeMillis is null
and groupId = ?
and assetId = ?`
)
.run(sessionUser.userName, Date.now(), groupId, assetId)

emileDB.close()

return result.changes > 0
}
3 changes: 3 additions & 0 deletions handlers/assets-post/doAddAssetGroupMember.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Request, Response } from 'express';
export declare function handler(request: Request, response: Response): void;
export default handler;
13 changes: 13 additions & 0 deletions handlers/assets-post/doAddAssetGroupMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { addAssetGroupMember } from '../../database/addAssetGroupMember.js';
import { getAssets } from '../../database/getAssets.js';
export function handler(request, response) {
const success = addAssetGroupMember(request.body.groupId, request.body.assetId, request.session.user);
const groupMembers = getAssets({
groupId: request.body.groupId
});
response.json({
success,
groupMembers
});
}
export default handler;
23 changes: 23 additions & 0 deletions handlers/assets-post/doAddAssetGroupMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Request, Response } from 'express'

import { addAssetGroupMember } from '../../database/addAssetGroupMember.js'
import { getAssets } from '../../database/getAssets.js'

export function handler(request: Request, response: Response): void {
const success = addAssetGroupMember(
request.body.groupId,
request.body.assetId,
request.session.user as EmileUser
)

const groupMembers = getAssets({
groupId: request.body.groupId
})

response.json({
success,
groupMembers
})
}

export default handler
3 changes: 3 additions & 0 deletions handlers/assets-post/doDeleteAssetGroupMember.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Request, Response } from 'express';
export declare function handler(request: Request, response: Response): void;
export default handler;
13 changes: 13 additions & 0 deletions handlers/assets-post/doDeleteAssetGroupMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { deleteAssetGroupMember } from '../../database/deleteAssetGroupMember.js';
import { getAssets } from '../../database/getAssets.js';
export function handler(request, response) {
const success = deleteAssetGroupMember(request.body.groupId, request.body.assetId, request.session.user);
const groupMembers = getAssets({
groupId: request.body.groupId
});
response.json({
success,
groupMembers
});
}
export default handler;
23 changes: 23 additions & 0 deletions handlers/assets-post/doDeleteAssetGroupMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Request, Response } from 'express'

import { deleteAssetGroupMember } from '../../database/deleteAssetGroupMember.js'
import { getAssets } from '../../database/getAssets.js'

export function handler(request: Request, response: Response): void {
const success = deleteAssetGroupMember(
request.body.groupId,
request.body.assetId,
request.session.user as EmileUser
)

const groupMembers = getAssets({
groupId: request.body.groupId
})

response.json({
success,
groupMembers
})
}

export default handler
1 change: 1 addition & 0 deletions public-typescript/assetGroups.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading

0 comments on commit 435ee92

Please sign in to comment.