cityssm/parking-ticket-system

View on GitHub
database/parkingDB/addParkingLocation.js

Summary

Maintainability
A
0 mins
Test Coverage
C
78%
import sqlite from 'better-sqlite3';
import { parkingDB as databasePath } from '../../data/databasePaths.js';
import { clearCacheByTableName } from '../../helpers/functions.cache.js';
export default function addParkingLocation(requestBody) {
    const database = sqlite(databasePath);
    const locationRecord = database
        .prepare(`select locationName, isActive
        from ParkingLocations
        where locationKey = ?`)
        .get(requestBody.locationKey);
    if (locationRecord !== undefined) {
        database.close();
        return {
            success: false,
            message: `The location key "${requestBody.locationKey}" is already associated with the ${locationRecord.isActive ? '' : 'inactive '} record "${locationRecord.locationName}".`
        };
    }
    const info = database
        .prepare(`insert into ParkingLocations (
        locationKey, locationName, locationClassKey, orderNumber, isActive)
        values (?, ?, ?, 0, 1)`)
        .run(requestBody.locationKey, requestBody.locationName, requestBody.locationClassKey);
    database.close();
    clearCacheByTableName('ParkingLocations');
    return {
        success: info.changes > 0
    };
}