"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "createDesignation", {
    enumerable: true,
    get: function() {
        return createDesignation;
    }
});
const _expressasynchandler = /*#__PURE__*/ _interop_require_default(require("express-async-handler"));
const _sequelize = require("sequelize");
const _zod = require("zod");
const _constants = require("@tamanu/constants");
const _errors = require("@tamanu/errors");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const createDesignationSchema = _zod.z.object({
    code: _zod.z.string().trim().min(1, {
        message: '`code` must be at least 1 character'
    }).max(255, {
        message: '`code` must be no longer than 255 characters'
    }),
    id: _zod.z.string().trim().min(1, {
        message: '`id` must be at least 1 character'
    }).max(255, {
        message: '`id` must be no longer than 255 characters'
    }),
    name: _zod.z.string().trim().min(1, {
        message: '`name` must be at least 1 character'
    }).max(65535, {
        message: '`name` must be no longer than 65,535 characters'
    })
});
const createDesignation = (0, _expressasynchandler.default)(async (req, res)=>{
    req.checkPermission('create', 'ReferenceData');
    const { ReferenceData } = req.store.models;
    const { code, id, name } = await createDesignationSchema.parseAsync(req.body);
    try {
        const designation = await ReferenceData.create({
            code,
            id,
            name,
            type: _constants.REFERENCE_TYPES.DESIGNATION
        });
        res.status(201).send(designation);
    } catch (err) {
        if (err instanceof _sequelize.UniqueConstraintError) {
            throw new _errors.DatabaseDuplicateError(`A reference datum already exists with ID ‘${id}’.`);
        }
        throw err;
    }
});

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