ldc-com-app/ComApp/src/App/Models/Directory.js
Jérémy Munsch 918e20a72b
SYNC 15 Major update, Formularies and Fixes
- Add type Definitions
- Fix models methods
- Sync controller add route names
- Formularies sync OK
- Closes https://lab.s-web.fr/ldc-web/projets/com-app-bo/-/issues/18
- Closes https://lab.s-web.fr/ldc-web/projets/com-app/-/issues/9
2023-09-04 10:08:53 +02:00

165 lines
3.7 KiB
JavaScript

export const DirectoriesSchema = {
name: 'Directories',
primaryKey: 'id',
properties: {
/** @type {number} */
id: 'int',
name: 'string',
/** @type {number|null} */
parent: { type: 'int', optional: true },
/** @type {number} */
order: 'int',
/** @type {number} */
needAuthorization: 'int',
},
};
/**
* @typedef {Unmanaged<typeof DirectoriesSchema.properties> & import('realm').Object} Directory
*/
class DirectoryModel {
/**
* DirectoryModel
* @param {Realm} realm
*/
constructor(realm) {
/** @type string */
this.schema = 'Directories';
/** @type {Realm} */
this.realm = realm;
}
/**
* @param {Directory} data
*/
add(data) {
this.realm.write(() => {
this.realm.create(this.schema, data);
});
}
/**
* @param {Directory} data
*/
update(data) {
this.realm.write(() => {
this.realm.create(this.schema, data, 'modified');
});
}
/**
* get directory with id
* @param {number} id
* @returns {Directory?}
*/
get(id) {
/** @type {import('realm').Results<Directory>} */
const tmp = this.realm.objects(this.schema);
if (tmp.length === 0) {
return null;
}
return tmp.filtered('id = ' + id)[0] ?? null;
}
/**
* @param {string} role
* @param {string} authorizedDirs JSON array of {Directory}
* @param {boolean} format
* @param {string} lang
* @returns {import('realm').Results<Directory>}
*/
getAll(role, authorizedDirs, format, lang) {
/** @type {Array<Directory>} */
const allowedDirs =
authorizedDirs !== null && authorizedDirs !== undefined
? JSON.parse(authorizedDirs)
: [];
/** @type {import('realm').Results<Directory>} */
let tmp = this.realm.objects(this.schema);
if (role === 'USER') {
if (allowedDirs.length > 0) {
tmp = tmp.filtered(
'needAuthorization = 0 OR ' +
allowedDirs
.map(directory => ' id = "' + directory.id + '"')
.join(' OR '),
);
} else {
tmp = tmp.filtered('needAuthorization = 0');
}
}
if (format) {
return this._recursiveDir(tmp, lang);
}
return tmp;
}
/** Delete all */
clear() {
this.realm.write(() => {
this.realm.delete(this.realm.objects(this.schema));
});
}
/**
* Recursive Dir filter
*
* @param {import('realm').Results<Directory>} list
* @param {string} lang
* @param {DirectoryData} parent
* @returns {DirectoryData[]}
*/
_recursiveDir(list, lang, parent = null) {
const root = list.filter(e => e.parent === parent);
const tmp = [];
for (let i = 0; i < root.length; i++) {
const childs = this._recursiveDir(list, lang, root[i].id);
/** @type {Object<string,string>} */
const name = JSON.parse(root[i].name);
tmp[i] = {};
tmp[i].id = root[i].id;
tmp[i].name = this.getI18nName(lang, name);
tmp[i].items = [];
for (let j = 0; j < childs.length; j++) {
childs[j].value = childs[j].id;
tmp[i].items.push(childs[j]);
}
if (!tmp[i].items.length) {
delete tmp[i].items;
}
}
return tmp;
}
/**
* Get i18n name
* @param {string} lang
* @param {Record<string,string>} name
* @returns {string}
*/
getI18nName(lang, name) {
/** @type {string} */
let tmp;
switch (lang) {
case 'fr':
tmp = name.fr;
break;
case 'en':
tmp = name.en;
break;
case 'de':
tmp = name.de;
break;
case 'es':
tmp = name.es;
break;
}
if (tmp === undefined) {
tmp = name.fr;
}
return tmp;
}
}
export default DirectoryModel;