unclean SPARC
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
class BypassTokensModel extends EICPluralModel {
|
||||
|
||||
freeTokens = 0;
|
||||
|
||||
constructor(privileges) {
|
||||
|
||||
super('tokens', privileges);
|
||||
|
||||
this.singletonClass = class {
|
||||
itemData = {
|
||||
id: null,
|
||||
granted: null,
|
||||
track: null,
|
||||
domain: null,
|
||||
subdomain: null,
|
||||
enterprise: null,
|
||||
project: null,
|
||||
history: null,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sanitize(item) {
|
||||
item.history.sort((a,b) => a.dateTime < b.dateTime ? 1: -1);
|
||||
item.status = item.history[0].status;
|
||||
for(let entry of item.history) {
|
||||
if(entry.actor && entry.actor.lastname == 'Application User') {
|
||||
entry.actor.lastname = entry.actor.firstname;
|
||||
entry.actor.firstname = null;
|
||||
}
|
||||
}
|
||||
item.enterprise.pic = item.enterprise.pic || '(not provided)';
|
||||
item.enterprise.legalname = item.enterprise.legalname || '(not provided)';
|
||||
// fuck PI
|
||||
if(item.project && item.project.submissiondate) {
|
||||
item.project.submissionDate = item.project.submissiondate;
|
||||
delete item.project.submissiondate;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} filters
|
||||
* @returns
|
||||
*/
|
||||
list(filters) {
|
||||
if(!this.hasPrivilege('list')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = this.getApiEndpoint('list');
|
||||
this.collection = [];
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, filters)
|
||||
.then( async serverData => {
|
||||
const payload = serverData.payload;
|
||||
this.freeTokens = payload.countOfTokens;
|
||||
this.fill(payload.tokens);
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} filters
|
||||
* @returns
|
||||
*/
|
||||
export(filters) {
|
||||
if(!this.hasPrivilege('list')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = this.getApiEndpoint('list');
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, filters)
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
getSettings(track) {
|
||||
if(!this.hasPrivilege('getSettings')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = this.getApiEndpoint('getSettings');
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, {track: track})
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
setSettings(payload) {
|
||||
if(!this.hasPrivilege('setSettings')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = this.getApiEndpoint('setSettings');
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, payload)
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
getPrograms(track, domain) {
|
||||
if(!this.hasPrivilege('getPrograms')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = app.config.api.programs['getPrograms'];
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, {domain: domain, track: track})
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
addProgram(payload) {
|
||||
if(!this.hasPrivilege('addProgram')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = app.config.api.programs['addProgram'];
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, payload)
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
removeProgram(payload) {
|
||||
if(!this.hasPrivilege('removeProgram')) return( new Promise((resolve, reject) => reject()))
|
||||
|
||||
let endpoint = app.config.api.programs['removeProgram'];
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, payload)
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the list of awarded tokens
|
||||
*
|
||||
* @returns array
|
||||
*/
|
||||
awardedTokens(year) {
|
||||
return (
|
||||
this.collection.filter(
|
||||
item => (['allocated','consumed'].includes(item.itemData.status))
|
||||
&& (new Date(item.itemData.granted).getFullYear() == year))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the list of consumed tokens
|
||||
*
|
||||
* @returns array
|
||||
*/
|
||||
consumedTokens(year) {
|
||||
return (
|
||||
this.collection.filter(
|
||||
item => (['consumed'].includes(item.itemData.status))
|
||||
&& (new Date(item.itemData.granted).getFullYear() == year))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a light-proposal for saving+submission+consume token
|
||||
*/
|
||||
viewToken(id) {
|
||||
if(!this.hasPrivilege('list')) return( new Promise((resolve, reject) => reject()))
|
||||
let endpoint = JSON.parse(JSON.stringify(app.config.api.tokens.view))
|
||||
endpoint.uri = endpoint.uri.replace('{tokenid}', id);
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method)
|
||||
.then( async serverData => {
|
||||
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a light-proposal for saving+submission+consume token
|
||||
*/
|
||||
consumeToken(payload) {
|
||||
if(!this.hasPrivilege('assign')) return( new Promise((resolve, reject) => reject()))
|
||||
let endpoint = app.config.api.tokens.assign
|
||||
endpoint.uri = endpoint.uri.replace('{pic}', payload.organisation.pic);
|
||||
|
||||
return (
|
||||
this.request(endpoint.uri, endpoint.method, payload)
|
||||
.then( async serverData => {
|
||||
return serverData.payload;
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the history (log) of all token actions
|
||||
*
|
||||
* @returns array
|
||||
*/
|
||||
history() {
|
||||
let dataset = [];
|
||||
for(let item of this.collection) {
|
||||
if(item.itemData.history.find(entry => entry.status == 'allocated')) {
|
||||
for(let entry of item.itemData.history) {
|
||||
if(entry.status != 'created' && entry.status != 'active') {
|
||||
dataset.push({
|
||||
date: entry.dateTime,
|
||||
pic: item.itemData.enterprise.pic,
|
||||
legalname: item.itemData.enterprise.legalname,
|
||||
action: entry.status,
|
||||
actor: entry.actor
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return dataset.sort((a,b) => a.date < b.date ? 1: -1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
app.registerClass('BypassTokensModel',BypassTokensModel);
|
||||
Reference in New Issue
Block a user