"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "createRole", {
    enumerable: true,
    get: function() {
        return createRole;
    }
});
const _expressasynchandler = /*#__PURE__*/ _interop_require_default(require("express-async-handler"));
const _sequelize = require("sequelize");
const _zod = require("zod");
const _errors = require("@tamanu/errors");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const createRoleSchema = _zod.z.object({
    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(255, {
        message: '`name` must be no longer than 255 characters'
    })
});
const createRole = (0, _expressasynchandler.default)(async (req, res)=>{
    req.checkPermission('create', 'Role');
    const { Role } = req.store.models;
    const { id, name } = await createRoleSchema.parseAsync(req.body);
    try {
        const role = await Role.create({
            id,
            name
        });
        res.status(201).send(role);
    } catch (err) {
        if (err instanceof _sequelize.UniqueConstraintError) {
            throw new _errors.DatabaseDuplicateError(`A role already exists with ID ‘${id}’`);
        }
        throw err;
    }
});

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