cityssm/corporate-records-manager

View on GitHub
helpers/recordsDB/cleanupRecordsTable.ts

Summary

Maintainability
A
0 mins
Test Coverage
import * as sqlPool from "@cityssm/mssql-multi-pool";
import * as configFns from "../configFns.js";

import type * as sqlTypes from "mssql";

import debug from "debug";
const debugSQL = debug("corporate-records-manager:recordsDB:cleanupRecordsTable");


export const cleanupRecordsTable = async (): Promise<number> => {

  try {
    const pool: sqlTypes.ConnectionPool =
      await sqlPool.connect(configFns.getProperty("mssqlConfig"));

    await pool.request()
      .query("delete" +
        " from CR.RelatedRecords" +
        " where recordID_A in (select recordID from CR.Records where recordDelete_datetime is not null)" +
        " or recordID_B in (select recordID from CR.Records where recordDelete_datetime is not null)");

    const result = await pool.request()
      .query("delete" +
        " from CR.Records" +
        " output deleted.recordID" +
        " where recordDelete_datetime is not null" +
        " and recordID not in (select recordID from CR.RecordTags)" +
        " and recordID not in (select recordID from CR.RecordStatusLog)" +
        " and recordID not in (select recordID from CR.RecordUsers)" +
        " and recordID not in (select recordID from CR.RecordURLs)" +
        " and recordID not in (select recordID_A from CR.RelatedRecords)" +
        " and recordID not in (select recordID_B from CR.RelatedRecords)" +
        " and recordID not in (select recordID from CR.RecordCommentLog)");

    if (result.recordset && result.recordset.length > 0) {
      return result.recordset.length;
    }

  } catch (error) {
    debugSQL(error);
  }

  return 0;
};


export default cleanupRecordsTable;