cityssm/attendance-tracking

View on GitHub
database/setEmployeeProperty.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { connect as sqlPoolConnect } from '@cityssm/mssql-multi-pool'

import { clearCacheByTableName } from '../helpers/functions.cache.js'
import { getConfigProperty } from '../helpers/functions.config.js'
import type { EmployeeProperty } from '../types/recordTypes.js'

export async function setEmployeeProperty(
  employeeProperty: EmployeeProperty,
  isSyncUpdate: boolean,
  sessionUser: AttendUser
): Promise<boolean> {
  const pool = await sqlPoolConnect(getConfigProperty('mssql'))

  let result = await pool
    .request()
    .input('propertyValue', employeeProperty.propertyValue)
    .input('isSyncUpdate', isSyncUpdate)
    .input('isSynced', employeeProperty.isSynced ?? false)
    .input('record_userName', sessionUser.userName)
    .input('record_dateTime', new Date())
    .input('employeeNumber', employeeProperty.employeeNumber)
    .input('propertyName', employeeProperty.propertyName)
    .query(`update MonTY.EmployeeProperties
      set propertyValue = case when (@isSyncUpdate = 1 and isSynced = 0 and recordDelete_dateTime is null) then propertyValue else @propertyValue end,
        isSynced = case when (@isSyncUpdate = 1 and isSynced = 0 and recordDelete_dateTime is null) then isSynced else @isSynced end,
        recordUpdate_userName = @record_userName,
        recordUpdate_dateTime = @record_dateTime,
        recordDelete_userName = null,
        recordDelete_dateTime = null
      where employeeNumber = @employeeNumber
        and propertyName = @propertyName`)

  if (result.rowsAffected[0] === 0) {
    result = await pool
      .request()
      .input('employeeNumber', employeeProperty.employeeNumber)
      .input('propertyName', employeeProperty.propertyName)
      .input('propertyValue', employeeProperty.propertyValue)
      .input('isSynced', employeeProperty.isSynced ?? false)
      .input('record_userName', sessionUser.userName)
      .input('record_dateTime', new Date())
      .query(`insert into MonTY.EmployeeProperties
        (employeeNumber, propertyName, propertyValue, isSynced,
          recordCreate_userName, recordCreate_dateTime, recordUpdate_userName, recordUpdate_dateTime)
        values (@employeeNumber, @propertyName, @propertyValue, @isSynced,
          @record_userName, @record_dateTime, @record_userName, @record_dateTime)`)
  }

  clearCacheByTableName('EmployeeProperties')

  return result.rowsAffected[0] > 0
}