Skip to content

Commit f40d526

Browse files
committed
Commissions variable schema links usage, schema verification function
1 parent 136a267 commit f40d526

2 files changed

Lines changed: 40 additions & 47 deletions

File tree

frontend/pages/commission.ejs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
</table>
5454
<div class="buttons">
5555
<a class="button" href="<%= tenant.domain %><%= tenant.path %>">Back</a>
56-
<% if (!commission.locked || (role !== 'user')) { %>
57-
<a class="button" href="<%= tenant.domain %><%= tenant.path %>/<%= commission.id %>/edit">Edit</a>
58-
<% } %>
56+
<% if (!commission.locked || (role !== 'user')) { %><a class="button" href="<%= tenant.domain %><%= tenant.path %>/<%= commission.id %>/edit">Edit</a><% } %>
57+
<% commission.links.forEach(link => { %><a class="button" href="<%= link.url %>" target="_blank" rel="noopener noreferrer"><%= link.label %></a><% }); %>
5958
</div>
6059
<%- include('foot.ejs') %>

index.js

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,48 @@ function getUserRole(session) {
118118
return 'user';
119119
};
120120

121+
function verifyAgainstSchema(type, data) {
122+
switch (type) {
123+
case 'commission':
124+
if (!Array.isArray(data)) return [];
125+
return data.map(commission => {
126+
if (!commission.id || !commission.user || (commission.date ? isNaN(new Date(commission.date).getTime()) : false) || (typeof commission.status !== 'string') || (typeof commission.tasks !== 'object')) return null;
127+
return {
128+
id: commission.id,
129+
user: commission.user,
130+
amount: commission.amount ? Number(commission.amount) : null,
131+
currency: commission.currency ? String(commission.currency) : 'USD',
132+
date: commission.date ? new Date(commission.date) : null,
133+
status: commission.status,
134+
fields: fields.reduce((acc, field) => {
135+
acc[field.id] = (commission.fields && (commission.fields[field.id] !== undefined)) ? commission.fields[field.id] : null;
136+
return acc;
137+
}, {}),
138+
tasks: Array.isArray(commission.tasks) ? commission.tasks.map(task => {
139+
return {
140+
done: task.done || false,
141+
content: task.content ? String(task.content) : ''
142+
}
143+
}) : [],
144+
locked: commission.locked || false,
145+
links: Array.isArray(commission.links) ? commission.links.map(link => {
146+
return {
147+
label: link.label ? String(link.label) : '',
148+
url: link.url ? String(link.url) : ''
149+
};
150+
}) : []
151+
};
152+
}).filter(commission => commission !== null);
153+
};
154+
return data;
155+
};
156+
121157
app.get('/', async (req, res) => {
122158
if (!on) return res.render('off', { tenant, title: 'Activation' });
123159
if (!req.session) return res.render('session', { tenant, title: 'Session' });
124160
if (!tenant.slug || !tenant.name || !tenant.domain) return res.render('tenant', { tenant, title: 'Configuration' });
125161
if (tenant.auth && tenant.auth.enabled && vars.userId && !req.session[vars.userId]) return res.render('auth', { tenant, title: 'Authenticate' });
126-
req.session[vars.commissions] = (req.session[vars.commissions] || []).map(commission => {
127-
if (!commission.id || !commission.user || (commission.date ? isNaN(new Date(commission.date).getTime()) : false) || (typeof commission.status !== 'string') || (typeof commission.tasks !== 'object')) return null;
128-
return {
129-
id: commission.id,
130-
user: commission.user,
131-
amount: commission.amount ? Number(commission.amount) : null,
132-
currency: commission.currency ? String(commission.currency) : 'USD',
133-
date: commission.date ? new Date(commission.date) : null,
134-
status: commission.status,
135-
fields: fields.reduce((acc, field) => {
136-
acc[field.id] = (commission.fields && (commission.fields[field.id] !== undefined)) ? commission.fields[field.id] : null;
137-
return acc;
138-
}, {}),
139-
tasks: (commission.tasks || []).map(task => {
140-
return {
141-
done: task.done || false,
142-
content: task.content ? String(task.content) : ''
143-
}
144-
}),
145-
locked: commission.locked || false
146-
};
147-
}).filter(commission => commission !== null);
162+
req.session[vars.commissions] = verifyAgainstSchema('commission', req.session[vars.commissions] || []);
148163
switch (getUserRole(req.session)) {
149164
case 'admin':
150165
return res.render('admin', { tenant, title: 'Admin View', session: req.session, vars });
@@ -212,28 +227,7 @@ app.get('/:id', async (req, res) => {
212227
if (!req.session) return res.render('session', { tenant, title: 'Session' });
213228
if (!tenant.slug || !tenant.name || !tenant.domain) return res.render('tenant', { tenant, title: 'Configuration' });
214229
if (tenant.auth && tenant.auth.enabled && vars.userId && !req.session[vars.userId]) return res.render('auth', { tenant, title: 'Authenticate' });
215-
req.session[vars.commissions] = (req.session[vars.commissions] || []).map(commission => {
216-
if (!commission.id || !commission.user || (commission.date ? isNaN(new Date(commission.date).getTime()) : false) || (typeof commission.status !== 'string') || (typeof commission.tasks !== 'object')) return null;
217-
return {
218-
id: commission.id,
219-
user: commission.user,
220-
amount: commission.amount ? Number(commission.amount) : null,
221-
currency: commission.currency ? String(commission.currency) : 'USD',
222-
date: commission.date ? new Date(commission.date) : null,
223-
status: commission.status,
224-
fields: fields.reduce((acc, field) => {
225-
acc[field.id] = (commission.fields && (commission.fields[field.id] !== undefined)) ? commission.fields[field.id] : null;
226-
return acc;
227-
}, {}),
228-
tasks: (commission.tasks || []).map(task => {
229-
return {
230-
done: task.done || false,
231-
content: task.content ? String(task.content) : ''
232-
}
233-
}),
234-
locked: commission.locked || false
235-
};
236-
}).filter(commission => commission !== null);
230+
req.session[vars.commissions] = verifyAgainstSchema('commission', req.session[vars.commissions] || []);
237231
if (getUserRole(req.session) === 'user') req.session[vars.commissions] = req.session[vars.commissions].filter(commission => commission.user === req.session[vars.userId]);
238232
const commission = (req.session[vars.commissions] || []).find(commission => (String(commission.id) === String(req.params.id)) && (commission.user === req.session[vars.userId]));
239233
if (!commission) return res.status(404).render('error', { tenant, title: 'Not Found', message: 'The requested commission was not found.' });

0 commit comments

Comments
 (0)