Files
P42_UI/app/models/submissions/SubmissionModel.js
T
2025-08-27 07:03:09 +00:00

337 lines
10 KiB
JavaScript

class SubmissionModel extends EICModel {
constructor(privileges) {
super('/organisations/{pic}/proposals/{pid}', privileges);
}
/**
* Sanitizes legacy short proposals form format
* @param {*} item
* @returns
*/
sanitizeLegacy(item) {
let merged = {...item.DiagnosticSubmission, ...item.Diagnostic};
this.data = item;
this.data.team = JSON.parse(JSON.stringify(item.DiagnosticTeamMembers));
this.data.UpdatedAt = item.DiagnosticSubmission.UpdatedAt;
this.data.resubmitted = item.DiagnosticSubmission.ResubmissionType ? true: false;
delete this.data.DiagnosticSubmission;
delete this.data.Diagnostic;
for(let key in merged) {
this.data[key] = merged[key];
}
return this.sanitize(this.data);
}
sanitize(item) {
if(['created', 'updated'].includes(item.status)) item.status = 'draft';
if(['evaluation-finalised'].includes(item.status)) item.status = 'evaluated';
if(['evaluation-contested'].includes(item.status)) item.status = 'contested';
return item;
}
getProposalLegacy(pic, number) {
//if(!this.hasPrivilege('read')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('read');
endpoint.uri = endpoint.uri.replace('{pic}', pic);
endpoint.uri = endpoint.uri.replace('{pid}', number);
/**/
return (
this.request('https://api.dev.eismea.eu/stable/bypass/legacy/short-proposals'+'?'+crypto.randomUUID(), 'POST', { params: { ProposalId: number}})
.then( async serverData => {
return this.sanitizeLegacy(serverData.payload);
})
)
}
getProposal(pic, number) {
if(!this.hasPrivilege('read')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('read');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number);
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method)
.then( async serverData => {
switch(serverData.payload.version) {
case '1.1':
case '1.0':
this.data = serverData.payload;
return this.sanitize(this.data);
default:
return this.sanitizeLegacy(serverData.payload);
}
})
)
}
/**
* Fetches latest company info from F&T portal
* @param {string} pic
* @returns
*/
getOrganisationInfo(pic) {
if(!this.hasPrivilege('update')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('organisationUpdate');
let uri = endpoint.uri.replace('{pic}', pic);
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method)
.then( async serverData => {
return serverData.payload;
})
)
}
/**
* Saves proposal form
* @param {*} number
* @param {*} payload
* @returns
*/
save(pic, number, form) {
if(!this.hasPrivilege('update')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('update');
endpoint.uri = endpoint.uri.replace('{pic}', pic);
endpoint.uri = endpoint.uri.replace('{pid}', number);
let payload = {
status: 'draft',
form: form
}
//this.merge(this.data, form);
return (
this.request(endpoint.uri+'?'+crypto.randomUUID(), endpoint.method, payload)
.then( async serverData => { return serverData.payload; })
)
}
/**
* Submits the short proposal
* @param {*} number
* @param {*} payload
*/
submit(pic,number, form) {
if(!this.hasPrivilege('update')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('update');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number);
let payload = {
status: 'submitting',
form: form
}
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method, payload)
.then( async serverData => { return serverData.payload; })
)
}
/**
* Withdraws the submitted short proposal
* @param {*} number
* @param {*} payload
*/
withdraw(pic,number, form) {
if(!this.hasPrivilege('withdraw')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('update');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number);
let payload = {
status: 'withdrawing',
form: form
}
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method, payload)
.then( async serverData => { return serverData.payload; })
)
}
clone(pic, number) {
if(!this.hasPrivilege('clone')) return ( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('clone');
let uri = endpoint.uri.replace('{pic}', pic);
let payload = {
sourceProposal: number
}
return (
this.request(uri, endpoint.method, payload)
.then( async serverData => { return serverData.payload })
)
}
/**
* Complains the evaluation of the short proposal
* @param {*} number
* @param {*} payload
*/
complain(pic,number, form) {
if(!this.hasPrivilege('complain')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('complain');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number);
/*
let payload = {
status: 'complaining',
form: form
}
*/
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method, form)
.then( async serverData => { return serverData.payload; })
)
}
}
app.registerClass('SubmissionModel', SubmissionModel);
class SubmissionTeamModel extends EICModel {
constructor(privileges) {
super('/organisations/{pic}/proposals/{pid}/team', privileges);
}
/**
* Adds a team member to the short proposal
* @param {*} member
* @returns
*/
create(pic, number, member) {
if(!this.hasPrivilege('create')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('create');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number);
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method, member)
.then( async serverData => { return serverData.payload; })
)
}
/**
* Updates a team member of the short proposal
* @param {*} member
* @returns
*/
update(pic, number, member) {
if(!this.hasPrivilege('update')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('update');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number).replace('{uid}', member.id);
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method, member)
.then( async serverData => { return serverData.payload; })
)
}
/**
* Removes a team member from the short proposal
* @param {*} id
* @returns
*/
delete(pic, number, id) {
if(!this.hasPrivilege('delete')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('delete');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', number).replace('{uid}', id);
return (
this.request(uri+'?'+crypto.randomUUID(), endpoint.method)
.then( async serverData => { return serverData.payload; })
)
}
}
app.registerClass('SubmissionTeamModel', SubmissionTeamModel);
class SubmissionMembersModel extends EICModel {
contributors = [];
constructor(privileges) {
super('/organisations/{pic}/proposals/{pid}/members', privileges);
}
/**
* Gets the contributors list on a proposal
* @param {string} pic
* @param {string} pid
* @returns
*/
list(pic,pid) {
if(!this.hasPrivilege('list')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('list');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', pid);
return (
this.request(uri, endpoint.method)
.then( async serverData => {
this.contributors = serverData.payload;
return this.contributors;
})
);
}
/**
* Grants to an organisation member access to the short proposal
* @param {string} pic
* @param {string} pid
* @param {string} uid
* @returns
*/
grant(pic, pid, uid) {
if(!this.hasPrivilege('grant')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('grant');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', pid).replace('{uid}', uid);
let proposalLink = '/organisations/{pic}/proposals/{pid}'.replace('{pic}',pic).replace('{pid}',pid)
return (
this.request(uri, endpoint.method, { status: 'active', proposalLink: proposalLink})
.then( async serverData => { return serverData.payload })
);
}
/**
* Revokes an organisation member access to the short proposal
* @param {string} pic
* @param {string} pid
* @param {string} uid
* @returns
*/
revoke(pic, pid, uid) {
if(!this.hasPrivilege('revoke')) return( new Promise((resolve, reject) => reject()))
let endpoint = this.getApiEndpoint('revoke');
let uri = endpoint.uri.replace('{pic}', pic).replace('{pid}', pid).replace('{uid}', uid);
let proposalLink = '/organisations/{pic}/proposals/{pid}'.replace('{pic}',pic).replace('{pid}',pid)
return (
this.request(uri, endpoint.method, { status: 'deleted', proposalLink: proposalLink })
.then( async serverData => { return serverData.payload })
);
}
}
app.registerClass('SubmissionMembersModel', SubmissionMembersModel);