cityssm/faster-web-helper

View on GitHub
modules/purchaseOrderApprovals/database/createUser.ts

Summary

Maintainability
A
2 hrs
Test Coverage
import sqlite from 'better-sqlite3'

import { getKeyGuid } from '../helpers/moduleHelpers.js'
import type { PurchaseOrderApprovalUser } from '../types/recordTypes.js'

import { databasePath } from './databaseHelpers.js'

export default function createUser(
  user: Partial<PurchaseOrderApprovalUser> & { userName: string }
): boolean {
  const database = sqlite(databasePath)

  const userRecord = database
    .prepare(
      `select isActive
        from Users
        where userName = ?`
    )
    .get(user.userName) as { isActive: boolean } | undefined

  if (userRecord === undefined) {
    database
      .prepare(
        `insert into Users
          (userName, fasterWebUserName, emailAddress, approvalMax, userKeyGuid, parentUserName, backupUserName, isAdmin, isActive)
          values (?, ?, ?, ?, ?, ?, ?, ?, ?)`
      )
      .run(
        user.userName,
        user.fasterWebUserName,
        user.emailAddress,
        user.approvalMax ?? 0,
        user.userKeyGuid ?? getKeyGuid(),
        user.parentUserName,
        user.backupUserName,
        user.isAdmin ?? 0,
        1
      )
  } else if (!userRecord.isActive) {
    database
      .prepare(
        `update Users
          set fasterWebUserName = ?,
          emailAddress = ?,
          approvalMax = ?,
          userKeyGuid = ?,
          parentUserName = ?,
          backupUserName = ?,
          isAdmin = ?,
          isActive = ?
          where userName = ?`
      )
      .run(
        user.fasterWebUserName,
        user.emailAddress,
        user.approvalMax ?? 0,
        user.userKeyGuid ?? getKeyGuid(),
        user.parentUserName,
        user.backupUserName,
        user.isAdmin ?? 0,
        1,
        user.userName
      )
  }

  database.close()

  return true
}