Notes/src/etapi/attributes.ts

86 lines
2.8 KiB
TypeScript
Raw Normal View History

import becca from "../becca/becca.js";
import eu from "./etapi_utils.js";
import mappers from "./mappers.js";
import attributeService from "../services/attributes.js";
import v from "./validators.js";
import type { Router } from "express";
import type { AttributeRow } from "../becca/entities/rows.js";
import type { ValidatorMap } from "./etapi-interface.js";
2024-04-07 15:15:50 +03:00
function register(router: Router) {
2025-01-09 18:07:02 +02:00
eu.route(router, "get", "/etapi/attributes/:attributeId", (req, res, next) => {
2022-01-10 17:09:20 +01:00
const attribute = eu.getAndCheckAttribute(req.params.attributeId);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapAttributeToPojo(attribute));
});
2024-04-07 15:15:50 +03:00
const ALLOWED_PROPERTIES_FOR_CREATE_ATTRIBUTE: ValidatorMap = {
2025-01-09 18:07:02 +02:00
attributeId: [v.mandatory, v.notNull, v.isValidEntityId],
noteId: [v.mandatory, v.notNull, v.isNoteId],
type: [v.mandatory, v.notNull, v.isAttributeType],
name: [v.mandatory, v.notNull, v.isString],
value: [v.notNull, v.isString],
isInheritable: [v.notNull, v.isBoolean],
position: [v.notNull, v.isInteger]
2022-01-12 19:32:23 +01:00
};
2025-01-09 18:07:02 +02:00
eu.route(router, "post", "/etapi/attributes", (req, res, next) => {
if (req.body.type === "relation") {
2022-01-12 19:32:23 +01:00
eu.getAndCheckNote(req.body.value);
2022-01-07 19:33:59 +01:00
}
2024-04-07 15:15:50 +03:00
const _params = {};
eu.validateAndPatch(_params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTRIBUTE);
const params: AttributeRow = _params as AttributeRow;
2022-01-07 19:33:59 +01:00
try {
const attr = attributeService.createAttribute(params);
res.status(201).json(mappers.mapAttributeToPojo(attr));
2025-01-09 18:07:02 +02:00
} catch (e: any) {
2022-01-12 19:32:23 +01:00
throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message);
2022-01-07 19:33:59 +01:00
}
});
const ALLOWED_PROPERTIES_FOR_PATCH_LABEL = {
2025-01-09 18:07:02 +02:00
value: [v.notNull, v.isString],
position: [v.notNull, v.isInteger]
2022-01-07 19:33:59 +01:00
};
const ALLOWED_PROPERTIES_FOR_PATCH_RELATION = {
2025-01-09 18:07:02 +02:00
position: [v.notNull, v.isInteger]
};
2025-01-09 18:07:02 +02:00
eu.route(router, "patch", "/etapi/attributes/:attributeId", (req, res, next) => {
2022-01-10 17:09:20 +01:00
const attribute = eu.getAndCheckAttribute(req.params.attributeId);
2022-01-07 19:33:59 +01:00
2025-01-09 18:07:02 +02:00
if (attribute.type === "label") {
eu.validateAndPatch(attribute, req.body, ALLOWED_PROPERTIES_FOR_PATCH_LABEL);
2025-01-09 18:07:02 +02:00
} else if (attribute.type === "relation") {
2022-01-12 19:32:23 +01:00
eu.getAndCheckNote(req.body.value);
eu.validateAndPatch(attribute, req.body, ALLOWED_PROPERTIES_FOR_PATCH_RELATION);
}
2022-01-12 19:32:23 +01:00
attribute.save();
2022-01-07 19:33:59 +01:00
res.json(mappers.mapAttributeToPojo(attribute));
});
2025-01-09 18:07:02 +02:00
eu.route(router, "delete", "/etapi/attributes/:attributeId", (req, res, next) => {
2022-01-07 19:33:59 +01:00
const attribute = becca.getAttribute(req.params.attributeId);
2023-06-05 09:23:42 +02:00
if (!attribute) {
2022-01-07 19:33:59 +01:00
return res.sendStatus(204);
}
attribute.markAsDeleted();
res.sendStatus(204);
});
}
export default {
2022-01-07 19:33:59 +01:00
register
2022-01-08 12:01:54 +01:00
};