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
Empty file added .config
Empty file.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGODB_CONNECTION_STRING=mongodb://127.0.0.1:27017/mongoose_test
1 change: 1 addition & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGODB_CONNECTION_STRING=mongodb://127.0.0.1:27017/mongoose_test
13 changes: 12 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

console.log('Config file is loading...');

if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'local';
}
Expand All @@ -11,7 +13,16 @@ const path = require('path');
console.log('NODE_ENV =', env);

if (!['development', 'production'].includes(process.env.NODE_ENV)) {
dotenv.config({
const result = dotenv.config({
path: path.resolve(__dirname, `.env.${env.toLocaleLowerCase()}`)
});
if (result.error) {
console.error('Error loading .env file:', result.error);
} else {
console.log('Loaded .env file:', path.resolve(__dirname, `.env.${env.toLocaleLowerCase()}`));
console.log('Environment variables:', {
MONGODB_CONNECTION_STRING: process.env.MONGODB_CONNECTION_STRING ? '[SET]' : '[NOT SET]',
NODE_ENV: process.env.NODE_ENV
});
}
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require('dotenv').config();

require('./config');

const cors = require('cors');
const connect = require('./src/db');
const express = require('express');
Expand Down
5 changes: 5 additions & 0 deletions netlify/functions/notifySlack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const extrovert = require('extrovert');

module.exports = extrovert.toNetlifyFunction(require('../../src/actions/notifySlack'));
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"scripts": {
"seed": "env NODE_ENV=development node ./seed",
"start": "node .",
"start:local": "env NODE_ENV=local node .",
"start:development": "env NODE_ENV=development node .",
"test": "env NODE_ENV=test mocha -r ./config test/*.test.js",
"lint": "eslint ."
}
Expand Down
78 changes: 78 additions & 0 deletions src/actions/notifySlack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

const Archetype = require('archetype');
const connect = require('../../src/db');
const slack = require('../integrations/slack');

const NotifySlackParams = new Archetype({
workspaceId: {
$type: 'string',
$required: true
},
modelName: {
$type: 'string',
$required: true
},
documentId: {
$type: 'string',
$required: true
},
purpose: {
$type: 'string',
$required: true
}
}).compile('NotifySlackParams');

module.exports = async function notifySlack(params) {
const { workspaceId, modelName, documentId, purpose } = new NotifySlackParams(params);

const db = await connect();
const { Workspace } = db.models;

const workspace = await Workspace.findById(workspaceId).orFail();

if (!workspace.slackWebhooks || workspace.slackWebhooks.length === 0) {
throw new Error('Workspace does not have any Slack webhook URLs configured');
}

const blocks = [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `A new document was created in the *${modelName}* model.\n*Document ID:* \`${documentId}\``
}
}
];

if (workspace.baseUrl) {
const url = `${workspace.baseUrl}/model/${modelName}/document/${documentId}`;
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `<${url}|View Document>`
}
});
}

const messagePayload = {
blocks
};

const matchingWebhooks = workspace.slackWebhooks.filter(webhook => {
return webhook.enabled !== false &&
webhook.purposes &&
webhook.purposes.includes(purpose);
});

if (matchingWebhooks.length === 0) {
throw new Error(`No enabled webhooks found for purpose: ${purpose}`);
}

await Promise.all(
matchingWebhooks.map(webhook => slack.sendWebhook(webhook.url, messagePayload))
);

return { success: true };
};
40 changes: 39 additions & 1 deletion src/db/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,45 @@ const workspaceSchema = new mongoose.Schema({
subscriptionTier: {
type: String,
enum: ['', 'free', 'pro']
}
},
slackWebhooks: [{
_id: false,
url: {
type: String,
required: true
},
name: {
type: String
},
purposes: [{
type: String,
required: true,
enum: ['documentCreated']
}],
enabled: {
type: Boolean,
default: true
}
}],
discordWebhooks: [{
_id: false,
url: {
type: String,
required: true
},
name: {
type: String
},
purposes: [{
type: String,
required: true,
enum: ['documentCreated']
}],
enabled: {
type: Boolean,
default: true
}
}]
}, { timestamps: true, id: false });

workspaceSchema.index({ apiKey: 1 }, { unique: true });
Expand Down
15 changes: 15 additions & 0 deletions src/integrations/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@ module.exports = {
const url = 'https://slack.com/api/chat.postMessage';
// Send to Slack
await axios.post(url, jobs, { headers: { authorization: `Bearer ${config.slackToken}` } });
},
async sendWebhook(webhookUrl, payload) {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});

if (!response.ok) {
throw new Error(`Slack webhook request failed with status ${response.status}`);
}

return response;
}
};
Loading