"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 getDesignationById () {
        return getDesignationById;
    },
    get getDesignationDeletabilityById () {
        return getDesignationDeletabilityById;
    },
    get getDesignations () {
        return getDesignations;
    }
});
const _expressasynchandler = /*#__PURE__*/ _interop_require_default(require("express-async-handler"));
const _lodash = require("lodash");
const _sequelize = require("sequelize");
const _constants = require("@tamanu/constants");
const _errors = require("@tamanu/errors");
const _crudHelpers = require("@tamanu/shared/utils/crudHelpers");
const _designationsdelete = require("./designations.delete");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getDesignations = (0, _expressasynchandler.default)(async (req, res)=>{
    req.checkPermission('list', 'ReferenceData');
    const idQuery = req.query.id?.trim();
    const nameQuery = req.query.name?.trim();
    const additionalFilters = {
        type: _constants.REFERENCE_TYPES.DESIGNATION
    };
    // Not matching word boundary; IDs often camel case
    if (idQuery) additionalFilters.id = {
        [_sequelize.Op.iLike]: `%${idQuery}%`
    };
    if (nameQuery) additionalFilters.name = {
        [_sequelize.Op.iRegexp]: `\\m${(0, _lodash.escapeRegExp)(nameQuery)}`
    };
    const response = await (0, _crudHelpers.getResourceList)(req, 'ReferenceData', '', {
        additionalFilters
    });
    res.send(response);
});
const getDesignationById = (0, _expressasynchandler.default)(async (req, res)=>{
    req.checkPermission('read', 'ReferenceData');
    const { store: { models: { ReferenceData } }, params: { id } } = req;
    const designation = await ReferenceData.findOne({
        where: {
            id,
            type: _constants.REFERENCE_TYPES.DESIGNATION
        }
    });
    if (!designation) {
        throw new _errors.NotFoundError(`No designation found with ID ‘${id}’`);
    }
    res.send(designation.forResponse());
});
const getDesignationDeletabilityById = (0, _expressasynchandler.default)(async (req, res)=>{
    req.checkPermission('delete', 'ReferenceData');
    const { store: { models, sequelize }, params: { id } } = req;
    await sequelize.transaction({
        readOnly: true
    }, async ()=>{
        await (0, _designationsdelete.getDeletableDesignationOrThrow)(models, id);
    });
    res.status(204).send();
});

//# sourceMappingURL=designations.get.js.map