Skip to content

Commit c72a6c2

Browse files
committed
Admin create commission on behalf of another user
1 parent 053ce60 commit c72a6c2

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ commtrackr.init({ // Initialize CommTracker with configurations
7575
},
7676
fields: [
7777
{
78-
id: 'name', // Unique identifier for the field
78+
id: 'name', // Unique identifier for the field. ID 'user' is reserved by the system and may not be used here
7979
type: 'text', // Field type ('text', 'number', 'date', 'textarea', 'checkbox', 'radio', 'select')
8080
label: 'Website Name', // Field label
8181
description: 'The name of the website or project.', // Field description

index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express');
22
const rateLimit = require('express-rate-limit');
33
const cors = require('cors');
44
const path = require('path');
5+
const { name } = require('ejs');
56
const app = express();
67
require('dotenv').config();
78

@@ -87,7 +88,7 @@ function init({
8788
commissions: 'commissions',
8889
...newVars
8990
};
90-
fields = newFields;
91+
fields = newFields.filter(field => field.id !== 'user');
9192
createHandler = newCreateHandler;
9293
updateHandler = newUpdateHandler;
9394
syncHandler = newSyncHandler;
@@ -176,7 +177,15 @@ app.get('/create', async (req, res) => {
176177
if (!req.session) return res.render('session', { tenant, title: 'Session' });
177178
if (!tenant.slug || !tenant.name || !tenant.domain) return res.render('tenant', { tenant, title: 'Configuration' });
178179
if (tenant.auth && tenant.auth.enabled && vars.userId && !req.session[vars.userId]) return res.render('auth', { tenant, title: 'Authenticate' });
179-
return res.render('create', { tenant, title: 'New Commission', session: req.session, vars, fields });
180+
return res.render('create', {
181+
tenant, title: 'New Commission', session: req.session, vars, fields: (getUserRole(req.session) === 'admin') ? [{
182+
id: 'user',
183+
label: 'User ID',
184+
description: 'The identifier of the user for whom this commission is created for, if any.',
185+
type: 'text',
186+
required: false
187+
}, ...fields] : fields
188+
});
180189
});
181190

182191
app.post('/create', async (req, res) => {
@@ -190,11 +199,15 @@ app.post('/create', async (req, res) => {
190199
if (field.id) data[field.id] = req.body[field.id] || null;
191200
});
192201
data.createdAt = new Date();
193-
data.createdBy = (tenant.auth && tenant.auth.enabled) ? {
202+
data.createdBy = (tenant.auth && tenant.auth.enabled) ? (((getUserRole(req.session) === 'admin') && req.body.user) ? {
203+
id: req.body.user,
204+
name: req.body.user,
205+
role: 'user'
206+
} : {
194207
id: req.session[vars.userId],
195-
name: req.session[vars.name] || req.session[vars.userId],
208+
name: req.session[vars.userName] || req.session[vars.userId],
196209
role: getUserRole(req.session) || 'user'
197-
} : {};
210+
}) : {};
198211
if (createHandler && typeof createHandler === 'function') {
199212
try {
200213
await createHandler(req, data);
@@ -265,7 +278,7 @@ app.post('/:id/edit', async (req, res) => {
265278
data.updatedAt = new Date();
266279
data.updatedBy = (tenant.auth && tenant.auth.enabled) ? {
267280
id: req.session[vars.userId],
268-
name: req.session[vars.name] || req.session[vars.userId],
281+
name: req.session[vars.userName] || req.session[vars.userId],
269282
role: getUserRole(req.session) || 'user'
270283
} : {};
271284
if (updateHandler && typeof updateHandler === 'function') {

0 commit comments

Comments
 (0)