Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ RABBITMQ_PORT=
# MODULES
FRONT_OFFICE_URI=
BACK_OFFICE_URI=

# MIXPANEL CONFIGURATION
MIXPANEL_TOKEN=
MIXPANEL_HOST=
6 changes: 6 additions & 0 deletions config/custom-environment-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
module.exports = {
server: {
port: 'SERVER_PORT',
url: 'SERVER_URL',
allowedOrigins: 'SERVER_ALLOWED_ORIGINS',
},
Expand Down Expand Up @@ -38,6 +39,7 @@ module.exports = {
clientId: 'AUTH_CLIENT_ID',
tenantId: 'AUTH_TENANT_ID',
allowedIssuers: 'AUTH_ALLOWED_ISSUERS',
audience: 'AUTH_AUDIENCE',
},
encryption: {
key: 'ENCRYPTION_KEY',
Expand All @@ -61,4 +63,8 @@ module.exports = {
clientSecret: 'COMMON_SERVICES_CLIENT_SECRET',
scope: 'COMMON_SERVICES_SCOPE',
},
mixpanel: {
token: 'MIXPANEL_TOKEN',
host: 'MIXPANEL_HOST',
},
};
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"@azure/storage-blob": "^12.5.0",
"@casl/ability": "^6.5.0",
"@casl/mongoose": "^7.2.1",
"@graphql-tools/schema": "^10.0.0",
"@turf/helpers": "^6.5.0",
"@turf/turf": "^6.5.0",
"@graphql-tools/schema": "^10.0.0",
"@ucast/mongo2js": "^1.3.3",
"amqplib": "^0.8.0",
"axios": "^1.4.0",
Expand Down Expand Up @@ -79,6 +79,7 @@
"lodash": "^4.17.21",
"migrate": "^1.8.0",
"mime-types": "^2.1.33",
"mixpanel": "^0.18.0",
"mongoose": "^7.4.3",
"node-cache": "^5.1.2",
"node-fetch": "^2.6.1",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import subscriberSafe from './server/subscriberSafe';
import pullJobScheduler from './server/pullJobScheduler';
import customNotificationScheduler from './server/customNotificationScheduler';
import { startDatabase } from './server/database';
import { initMixpanel } from './server/mixpanel';
import config from 'config';
import { logger } from './services/logger.service';
import { checkConfig } from '@utils/server/checkConfig.util';
Expand All @@ -22,6 +23,9 @@ declare global {
}
}

/** Init Mixpanel */
initMixpanel();

// Ensure that all mandatory keys exist
checkConfig();

Expand Down
2 changes: 2 additions & 0 deletions src/models/form.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface FormDocument extends Document {
versions?: any[];
channel?: any;
layouts?: any;
logEvents?: boolean;
}

/** Interface of form */
Expand Down Expand Up @@ -82,6 +83,7 @@ const schema = new Schema<Form>(
graphQLTypeName: String,
structure: mongoose.Schema.Types.Mixed,
core: Boolean,
logEvents: Boolean,
status: {
type: String,
enum: Object.values(status),
Expand Down
54 changes: 51 additions & 3 deletions src/routes/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { formatFilename } from '@utils/files/format.helper';
import { sendEmail } from '@utils/email';
import exportBatch from '@utils/files/exportBatch';
import { accessibleBy } from '@casl/mongoose';
import { downloadFormFileEvent } from '@server/mixpanel';

/**
* Exports files in csv or xlsx format, excepted if specified otherwise
Expand Down Expand Up @@ -117,7 +118,19 @@ router.get('/form/records/:id', async (req, res) => {
);
// If the export is only of a template, build and export it, else build and export a file with the records
if (req.query.template) {
return await templateBuilder(res, form.name, columns);
const file = await templateBuilder(res, form.name, columns);
// Log event
if (form.logEvents) {
const user = req.context.user;
downloadFormFileEvent(
form,
form.name,
user,
null,
'Export of the template of the form to upload new records.'
);
}
return file;
} else {
const records = await Record.find(filter);
const rows = await getRows(
Expand All @@ -131,7 +144,19 @@ router.get('/form/records/:id', async (req, res) => {
});
const type = (req.query ? req.query.type : 'xlsx').toString();
const filename = formatFilename(form.name);
return await fileBuilder(res, filename, columns, rows, type);
const file = await fileBuilder(res, filename, columns, rows, type);
// Log event
if (form.logEvents) {
const user = req.context.user;
downloadFormFileEvent(
form,
filename,
user,
null,
'Export of the records of the form'
);
}
return file;
}
} else {
return res.status(404).send(i18next.t('common.errors.dataNotFound'));
Expand Down Expand Up @@ -259,7 +284,19 @@ router.get('/form/records/:id/history', async (req, res) => {
dateLocale,
type,
};
return await historyFileBuilder(res, history, meta, options);
const file = await historyFileBuilder(res, history, meta, options);
// Log event
if (form.logEvents) {
const user = req.context.user;
downloadFormFileEvent(
form,
'Record history ' + meta.record,
user,
meta,
'Export versions of the record'
);
}
return file;
} else {
return res.status(404).send(req.t('common.errors.dataNotFound'));
}
Expand Down Expand Up @@ -549,6 +586,17 @@ router.get('/file/:form/:blob/:record/:field', async (req, res) => {
const path = `files/${sanitize(req.params.blob)}`;
await downloadFile('forms', blobName, path);
res.download(path, () => {
// Log event
if (form.logEvents) {
const user = req.context.user;
downloadFormFileEvent(
form,
blobName,
user,
null,
'Export another type of file from blob storage. Path: ' + path
);
}
fs.unlink(path, () => {
logger.info('file deleted');
});
Expand Down
9 changes: 8 additions & 1 deletion src/schema/mutation/addRecord.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import { logger } from '@services/logger.service';
import { graphQLAuthCheck } from '@schema/shared';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';
import { recordEvent } from '@server/mixpanel';

/** Arguments for the addRecord mutation */
type AddRecordArgs = {
export type AddRecordArgs = {
form?: string | Types.ObjectId;
data: any;
};
Expand Down Expand Up @@ -133,6 +134,12 @@ export default {
publisher.publish(channel.id, { notification });
}
await record.save();

// Log event
if (form.logEvents) {
recordEvent('Add record', form, record, user, args);
}

return record;
} catch (err) {
logger.error(err.message, { stack: err.stack });
Expand Down
42 changes: 39 additions & 3 deletions src/schema/mutation/convertRecord.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { logger } from '@services/logger.service';
import { graphQLAuthCheck } from '@schema/shared';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';
import { recordEvent } from '@server/mixpanel';

/** Arguments for the convertRecord mutation */
type ConvertRecordArgs = {
export type ConvertRecordArgs = {
id: string | Types.ObjectId;
form: string | Types.ObjectId;
copyRecord: boolean;
Expand Down Expand Up @@ -90,13 +91,48 @@ export default {
name: targetForm.name,
},
});
return await targetRecord.save();

const record = await targetRecord.save();

// Log event
if (oldForm.logEvents || targetForm.logEvents) {
recordEvent(
'Convert record',
targetForm,
record,
user,
args,
oldRecord,
'Convert record coping record to the target form (record in the old form still exists)',
oldForm
);
}

return record;
} else {
const update: any = {
form: args.form,
//modifiedAt: new Date(),
};
return await Record.findByIdAndUpdate(args.id, update, { new: true });
const record = await Record.findByIdAndUpdate(args.id, update, {
new: true,
});

// Log event
if (oldForm.logEvents || targetForm.logEvents) {
recordEvent(
'Convert record',
targetForm,
record,
user,
args,
oldRecord,
'Convert record without copy record (overwriting record)',
oldForm
);
}

return record;
}
} catch (err) {
logger.error(err.message, { stack: err.stack });
Expand Down
37 changes: 34 additions & 3 deletions src/schema/mutation/deleteRecord.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { logger } from '@services/logger.service';
import { graphQLAuthCheck } from '@schema/shared';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';
import { recordEvent } from '@server/mixpanel';

/** Arguments for the deleteRecord mutation */
type DeleteRecordArgs = {
export type DeleteRecordArgs = {
id: string | Types.ObjectId;
hardDelete?: boolean;
};
Expand Down Expand Up @@ -47,13 +48,43 @@ export default {

// Delete the record
if (args.hardDelete) {
return await Record.findByIdAndDelete(record._id);
const deletion = await Record.findByIdAndDelete(record._id);

// Log event
if (form.logEvents) {
recordEvent(
'Delete record',
form,
record,
user,
args,
null,
'Record hard deleted'
);
}

return deletion;
} else {
return await Record.findByIdAndUpdate(
const deletion = await Record.findByIdAndUpdate(
record._id,
{ archived: true },
{ new: true }
);

// Log event
if (form.logEvents) {
recordEvent(
'Delete record',
form,
record,
user,
args,
null,
'Record archived (soft deleted)'
);
}

return deletion;
}
} catch (err) {
logger.error(err.message, { stack: err.stack });
Expand Down
Loading