impr: Non usare i : per campi id relations

This commit is contained in:
Maicol Battistini 2022-01-27 21:35:10 +01:00
parent a66e511798
commit 820a2d7a1a
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
1 changed files with 9 additions and 17 deletions

View File

@ -378,7 +378,6 @@ export class RecordsPage extends Page {
}
}
// eslint-disable-next-line sonarjs/cognitive-complexity
async setter(model: IModel, data: Collection<File | string>) {
const firstFields = data.only(this.fieldsPrecedence);
const fields = data.except(this.fieldsPrecedence);
@ -390,25 +389,18 @@ export class RecordsPage extends Page {
const relations: Record<string, IModel> = {};
for (const [field, value] of Object.entries(data.all())) {
if (field.includes(':')) {
let [relation, fieldName]: (string | undefined)[] = field.split(':');
let relationModel: IModel;
if ((model as unknown as typeof Model).relationships.includes(field)) {
relations[field] = await this.getRelation(model, field, false, Number(value)) as IModel;
}
if (fieldName && !relation) {
// If the field is a model id, we need to save the relation and not the field itself
relation = fieldName;
fieldName = undefined;
relationModel = await this.getRelation(model, relation, false, Number(value)) as IModel;
} else {
relationModel = relation in relations
? relations[relation]
: await this.getRelation(model, relation, true) as IModel;
}
if (field.includes(':')) {
const [relation, fieldName]: (string | undefined)[] = field.split(':');
const relationModel: IModel = relation in relations
? relations[relation]
: await this.getRelation(model, relation, true) as IModel;
if (relationModel) {
if (fieldName) {
relationModel[fieldName] = value;
}
relationModel[fieldName] = value;
relations[relation] = relationModel;
}
} else {