certificate-manager/functions/structure.mjs
2025-05-17 16:10:17 +02:00

92 lines
1.9 KiB
JavaScript

import {
Op
} from "sequelize";
import {
ElementTable,
AttributeTable
} from "../database/models/structure.mjs";
export async function CreateDirectory(parentId, name)
{
let ParentDirectory = await ElementTable.findByPk(parentId);
if (ParentDirectory == null) {
var NewDirectory = await ElementTable.create({
type: 'directory'
});
} else {
var NewDirectory = await ParentDirectory.createChild({
type: 'directory'
});
}
await NewDirectory.createAttribute({
version: NewDirectory.latestVersion,
contentType: 'name',
contentValue: name
})
return NewDirectory;
}
export async function GetDirectory(directoryId, version = null)
{
let Directory = await ElementTable.findByPk(directoryId);
if (Directory == null) {
return null;
}
if (version == null) {
version = Directory.latestVersion
}
let Attributes = await Directory.getAttribute()
var Result = {
id: Directory.id,
version
}
Attributes.forEach((Attribute) => {
Result[Attribute.contentType] = Attribute.contentValue
});
return Result;
}
export async function GetDirectories(parentId = null)
{
let Directories = await ElementTable.findAll({
where: {
[Op.and]: {
type: 'directory',
parentId: parentId
}
}
});
Directories = Directories.map(async (Directory) => {
let Attributes = await Directory.getAttribute()
var Result = {
id: Directory.id,
version: Directory.latestVersion
}
console.log(Attributes);
Attributes.forEach((Attribute) => {
Result[Attribute.contentType] = Attribute.contentValue
});
return Result;
})
await Promise.all(Directories);
return Directories;
}