"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get down () {
        return down;
    },
    get up () {
        return up;
    }
});
const _sequelize = require("sequelize");
const _config = /*#__PURE__*/ _interop_require_default(require("config"));
const _timeZoneCheck = require("@tamanu/shared/utils/timeZoneCheck");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const ISO9075_DATE_TIME_FMT = 'YYYY-MM-DD HH24:MI:SS';
async function up(query) {
    const PRIMARY_TIME_ZONE = (0, _timeZoneCheck.getPrimaryTimeZone)(_config.default);
    if (!PRIMARY_TIME_ZONE) {
        throw Error('A primaryTimeZone must be configured in local.json for this migration to run.');
    }
    await query.addColumn('lab_requests', 'published_date', {
        type: _sequelize.DataTypes.DATETIMESTRING,
        allowNull: true
    });
    await query.sequelize.query(`
  UPDATE lab_requests
    SET published_date = TO_CHAR(lrl.created_at::TIMESTAMPTZ AT TIME ZONE '${PRIMARY_TIME_ZONE}', '${ISO9075_DATE_TIME_FMT}')
    FROM lab_request_logs lrl
    WHERE lrl.lab_request_id = lab_requests.id AND lrl.status = 'published';
  `);
}
async function down(query) {
    await query.removeColumn('lab_requests', 'published_date');
}

//# sourceMappingURL=1682409691120-addPublishedDateLabRequest.js.map