metis-desk/modules/assets/src/api/infrastructure/persistence/asset.model.ts
David 37110d87ed feat: add optional registeredOn field to asset model and update related components
- Introduced a new optional field `registeredOn` to the asset input schema.
- Updated asset values and persistence layers to handle the new field.
- Enhanced tests to validate the behavior of the `registeredOn` field, including various date formats and null values.
- Modified UI components to display the registration date in asset details and forms.
- Added localization support for the new field in English and Spanish.
- Implemented helper functions for formatting the registration date.
- Created integration tests for reading and writing assets with the new field in the database.
2026-10-15 18:39:37 +02:00

56 lines
1.7 KiB
TypeScript

import type { Sequelize } from "sequelize";
import { DataTypes, QueryTypes } from "sequelize";
import { inventoryColumns, specificationColumns } from "./asset-relational-schema";
const id = { type: DataTypes.UUID, primaryKey: true };
const fk = (model: string, nullable = false) => ({
type: DataTypes.UUID,
allowNull: nullable,
references: { model, key: "id" },
onDelete: "RESTRICT",
});
export async function migrateInventory(database: Sequelize) {
const query = database.getQueryInterface();
const tables = await database.query<{ name: string }>(
"SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE()",
{ type: QueryTypes.SELECT }
);
const asset = {
id,
companyId: fk("companies"),
templateId: fk("asset_definitions"),
siteId: fk("company_sites", true),
brandId: fk("asset_brands", true),
...inventoryColumns,
...specificationColumns,
};
const revision = {
id,
assetId: fk("assets"),
actorId: fk("users"),
siteId: fk("company_sites", true),
brandId: fk("asset_brands", true),
recordedAt: { type: DataTypes.STRING(30), allowNull: false },
...inventoryColumns,
templateId: fk("asset_definitions"),
position: { type: DataTypes.INTEGER, allowNull: false },
};
if (!tables.some((table) => table.name === "assets")) {
await query.createTable("assets", asset);
}
if (!tables.some((table) => table.name === "asset_revisions")) {
await query.createTable("asset_revisions", revision);
}
for (const table of ["assets", "asset_revisions"]) {
const columns = await query.describeTable(table);
if (!("registeredOn" in columns)) {
await query.addColumn(table, "registeredOn", inventoryColumns.registeredOn);
}
}
}