Skip to content

Commit b29fbb9

Browse files
committed
Add multiple handlers, update schema replace commission user ID property
1 parent d4c482b commit b29fbb9

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,23 @@ commtrackr.init({ // Initialize CommTracker with configurations
8888
],
8989
},
9090
],
91-
handler: (data) => {
92-
// Custom handler function for processing commission data
93-
// This function is called when a commission is created or updated
94-
// You can implement your own logic here, such as saving to a database
95-
}
91+
handlers: {
92+
create: (req, data) => {
93+
// Custom handler function for processing commission data
94+
// This function is called when a commission is created
95+
// You can implement your own logic here, such as saving to a database
96+
},
97+
update: (req, data) => {
98+
// Custom handler function for updating commission data
99+
// This function is called when a commission is updated
100+
// You can implement your own logic here, such as saving to a database
101+
},
102+
sync: (req) => {
103+
// Custom handler function for syncing user's commissions
104+
// This function is called when the user manually triggers a sync
105+
// You can implement your own logic here, such as syncing your commissions session variable
106+
},
107+
},
96108
});
97109

98110
app.listen(3000, () => {
@@ -178,7 +190,7 @@ Session Example:
178190
[
179191
{
180192
id: 'unique-commission-id', // Unique identifier for the commission
181-
client: 'Client Name', // Name of the client
193+
user: 'userId', // Commission creator's unique userId. Should match the userId variable, otherwise the commission will only be accessible in admin/dev views
182194
amount: 1000, // Commission amount, or null if not applicable
183195
currency: 'USD', // Currency code for the commission amount
184196
date: '2023-10-01', // Date of the commission

index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ let on = false;
2525
let tenant = {};
2626
let vars = {};
2727
let fields = [];
28-
let returnHandler = null;
28+
let createHandler = null;
29+
let updateHandler = null;
30+
let syncHandler = null;
2931

3032
function init({
3133
tenant: newTenant = {
@@ -52,7 +54,11 @@ function init({
5254
commissions: 'commissions'
5355
},
5456
fields: newFields = [],
55-
handler: newHandler = null
57+
handlers: {
58+
newCreateHandler = null,
59+
newUpdateHandler = null,
60+
newSyncHandler = null
61+
}
5662
}) {
5763
tenant = {
5864
slug: 'commtrackr',
@@ -80,7 +86,9 @@ function init({
8086
...newVars
8187
};
8288
fields = newFields;
83-
returnHandler = newHandler;
89+
createHandler = newCreateHandler;
90+
updateHandler = newUpdateHandler;
91+
syncHandler = newSyncHandler;
8492
};
8593

8694
function activate(isOn = true) {
@@ -150,9 +158,9 @@ app.post('/create', async (req, res) => {
150158
name: req.session[vars.name] || req.session[vars.userId],
151159
role: getUserRole(req.session) || 'user'
152160
} : {};
153-
if (returnHandler && typeof returnHandler === 'function') {
161+
if (createHandler && typeof createHandler === 'function') {
154162
try {
155-
await returnHandler(data);
163+
await createHandler(req, data);
156164
} catch (error) {
157165
console.error('Error in handler function:', error);
158166
return res.status(500).json({ status: 'error', message: 'An error occurred while processing your request. Please try again later.' });

0 commit comments

Comments
 (0)