Skip to content

Commit

Permalink
reports to jira instead of discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Feuerhamster committed Aug 7, 2024
1 parent a47617b commit 0522591
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/controllers/entries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { config } from "../services/config.service.js"
import * as EntryService from "../services/entry.service.js"
import * as Database from "../services/database.service.js"
import * as DiscordService from "../services/discord.service.js"
import * as AtlassianService from "../services/atlassian.service.js"

import queryNumberParser from "../middleware/queryNumberParser.middleware.js"
import queryArrayParser from "../middleware/queryArrayParser.middleware.js"
Expand Down Expand Up @@ -57,7 +58,8 @@ export default class EntriesController {

res.status(StatusCode.Created).end();

DiscordService.sendNewEntryNotification(req.body.name, req.body.type);
// DiscordService.sendNewEntryNotification(req.body.name, req.body.type);
AtlassianService.newEntryTicket(req.body.name);
}

@Get("unapproved")
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import validate from "../middleware/validation.middleware.js"

import { config } from "../services/config.service.js"
import * as Database from "../services/database.service.js"
import * as Discord from "../services/discord.service.js"
import * as Atlassian from "../services/atlassian.service.js"


const newReportLimiter = rateLimit({
Expand All @@ -27,7 +27,7 @@ export default class ReportController {

if (!entry) return res.status(StatusCode.NotFound).send({ error: "entry_not_found" }).end();

let sent = await Discord.sendReport(entry, req.body.type, req.body.message);
let sent = await Atlassian.createReportTicket(entry, req.body.type, req.body.message);

if (!sent) return res.status(StatusCode.InternalServerError).end();

Expand Down
17 changes: 17 additions & 0 deletions src/models/entryMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import IDictionary from "../types/dictionary"

export const typeMapping: IDictionary = {
group: "Gruppe/Verein",
therapist: "Therapeut*in/Psychiater*in",
surveyor: "Gutachter*in",
endocrinologist: "Endokrinologische Praxis",
surgeon: "Operateur*in",
logopedics: "Logopäd*in",
hairremoval: "Haarentfernung"
}

export const reportTypeMapping = {
edit: "Änderungsvorschlag",
report: "Nicht empfehlenswert",
other: "Sonstiges"
}
79 changes: 79 additions & 0 deletions src/services/atlassian.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import axios from "axios";
import { DatabaseEntry } from "../models/database/entry.model";
import { reportTypeMapping } from "../models/entryMapping";
import IDictionary from "../types/dictionary";
import { config } from "./config.service.js";

export async function createReportTicket(entry: DatabaseEntry<"out">, type: keyof typeof reportTypeMapping, message: string) {
const customFields = new Map<string, unknown>();
customFields.set(config.atlassian.customFieldReportType, { id: config.atlassian.customfieldReportTypeMapping[type] });
customFields.set(config.atlassian.customfieldURL, config.reportEntryURL + entry._id);

const body: IDictionary = {
fields: {
issuetype: {
id: config.atlassian.issueTypes.report
},
summary: entry.name,
project: {
id: config.atlassian.projectId,
},
description: {
content: [
{
content: [
{
text: message,
type: "text"
}
],
type: "paragraph"
}
],
type: "doc",
version: 1
},
...Object.fromEntries(customFields.entries())
}
}

try {
await axios.post(config.atlassian.apiURL, body, {
auth: {
username: config.atlassian.username,
password: config.atlassian.key,
}
});
return true;
} catch (e: any) {
console.error(`Error while creating atlassian jira ticket! Status Code: ${e.message}`);
return false;
}
}

export async function newEntryTicket(name: string) {
const body: IDictionary = {
fields: {
issuetype: {
id: config.atlassian.issueTypes.newEntry
},
summary: name,
project: {
id: config.atlassian.projectId,
}
}
}

try {
await axios.post(config.atlassian.apiURL, body, {
auth: {
username: config.atlassian.username,
password: config.atlassian.key,
}
});
return true;
} catch (e: any) {
console.error(`Error while creating atlassian jira ticket! Status Code: ${e.message}`);
return false;
}
}
19 changes: 18 additions & 1 deletion src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@ const defaultConfig = {
}
},
discordWebhookURL: "",
reportEntryURL: "https://transdb.de/manage/database?id="
reportEntryURL: "https://transdb.de/manage/database?id=",
atlassian: {
apiURL: "",
username: "",
key: "",
projectId: "",
customfieldReportTypeMapping: {
edit: "10026",
report: "10027",
other: "10028"
},
issueTypes: {
report: "10003",
newEntry: "10014"
},
customfieldURL: "customfield_10040",
customFieldReportType: "customfield_10041"
}
};

const configPath = "./config.json";
Expand Down
18 changes: 1 addition & 17 deletions src/services/discord.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
import axios from "axios"
import { config } from "./config.service.js"
import { DatabaseEntry } from "../models/database/entry.model.js"
import IDictionary from "../types/dictionary"

const typeMapping: IDictionary = {
group: "Gruppe/Verein",
therapist: "Therapeut*in/Psychiater*in",
surveyor: "Gutachter*in",
endocrinologist: "Endokrinologische Praxis",
surgeon: "Operateur*in",
logopedics: "Logopäd*in",
hairremoval: "Haarentfernung"
}

const reportTypeMapping = {
edit: "Änderungsvorschlag",
report: "Nicht empfehlenswert",
other: "Sonstiges"
}
import { reportTypeMapping, typeMapping } from "../models/entryMapping.js";

/**
* Send a Discord webhook to notify the creation of a new entry
Expand Down

0 comments on commit 0522591

Please sign in to comment.