Skip to content

Commit

Permalink
adding some logging for messages sent. #29
Browse files Browse the repository at this point in the history
  • Loading branch information
kapil1garg committed May 8, 2024
1 parent cffb819 commit 801eb35
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 2 additions & 3 deletions controllers/programmingLanguage/feedbackFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ const sendSlackMessageToProjectChannel = async function ({
message: eval('`' + message + '`'),
});
} catch (error) {
console.error(`Error sendSlackMessageToProjectChannel:`);
console.error(error);
console.error(`Error sendSlackMessageToProjectChannel: ${error.stack}`);
}
};

Expand All @@ -157,7 +156,7 @@ const sendSlackMessageToSigChannel = async function ({ sigName, message }) {
message: eval('`' + message + '`'),
});
} catch (error) {
console.error(`Error sendSlackMessageToProjectChannel: ${error}`);
console.error(`Error sendSlackMessageToProjectChannel: ${error.stack}`);
}
};

Expand Down
25 changes: 25 additions & 0 deletions imports/studioAPI/requests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import got from 'got';
import { studioAPIUrl } from '../../index.js';
import { MessageLog } from '../../models/messageLog.js';

/**
* GET request wrapper for the Studio API.
Expand Down Expand Up @@ -35,6 +36,30 @@ export const postToStudioApi = async (
jsonBody = {},
retryLimit = 3
) => {
// create message log
let newMessageLog = new MessageLog({
message: jsonBody.message,
});

if (jsonBody.sigName) {
newMessageLog.target = {
type: 'sig',
sigName: jsonBody.sigName,
};
} else if (jsonBody.projName) {
newMessageLog.target = {
type: 'project',
projectName: jsonBody.projName,
};
} else if (jsonBody.people) {
newMessageLog.target = {
type: 'people',
people: jsonBody.people,
};
}

await newMessageLog.save();

return got.post(`${studioAPIUrl}/${subdomain}`, {
json: jsonBody,
responseType: 'json',
Expand Down
38 changes: 38 additions & 0 deletions models/messageLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* This module provides a schema for the message log. Message logs are saved anytime a message is sent to a user by Slack. This is useful for tracking messages sent to users.
*/

import mongoose from 'mongoose';

/**
* Model for the message log.
* @type {Model<any>}
*/
export const MessageLog = mongoose.model(
'MessageLog',
new mongoose.Schema({
timestamp: {
type: Date,
default: Date.now,
},
target: {
type: {
type: String,
enum: ['project', 'sig', 'people'],
required: true,
},
projectName: { type: String, required: false, default: '' },
sigName: { type: String, required: false, default: '' },
people: [{ type: String, required: false, default: '' }],
},
message: {
type: String,
required: true,
},
error: {
type: String,
required: false,
default: '',
},
})
);

0 comments on commit 801eb35

Please sign in to comment.