1
+ import axios from "axios" ;
2
+ import { DatabaseEntry } from "../models/database/entry.model" ;
3
+ import { reportTypeMapping } from "../models/entryMapping" ;
4
+ import IDictionary from "../types/dictionary" ;
5
+ import { config } from "./config.service.js" ;
6
+
7
+ export async function createReportTicket ( entry : DatabaseEntry < "out" > , type : keyof typeof reportTypeMapping , message : string ) {
8
+ const customFields = new Map < string , unknown > ( ) ;
9
+ customFields . set ( config . atlassian . customFieldReportType , { id : config . atlassian . customfieldReportTypeMapping [ type ] } ) ;
10
+ customFields . set ( config . atlassian . customfieldURL , config . reportEntryURL + entry . _id ) ;
11
+
12
+ const body : IDictionary = {
13
+ fields : {
14
+ issuetype : {
15
+ id : config . atlassian . issueTypes . report
16
+ } ,
17
+ summary : entry . name ,
18
+ project : {
19
+ id : config . atlassian . projectId ,
20
+ } ,
21
+ description : {
22
+ content : [
23
+ {
24
+ content : [
25
+ {
26
+ text : message ,
27
+ type : "text"
28
+ }
29
+ ] ,
30
+ type : "paragraph"
31
+ }
32
+ ] ,
33
+ type : "doc" ,
34
+ version : 1
35
+ } ,
36
+ ...Object . fromEntries ( customFields . entries ( ) )
37
+ }
38
+ }
39
+
40
+ try {
41
+ await axios . post ( config . atlassian . apiURL , body , {
42
+ auth : {
43
+ username : config . atlassian . username ,
44
+ password : config . atlassian . key ,
45
+ }
46
+ } ) ;
47
+ return true ;
48
+ } catch ( e : any ) {
49
+ console . error ( `Error while creating atlassian jira ticket! Status Code: ${ e . message } ` ) ;
50
+ return false ;
51
+ }
52
+ }
53
+
54
+ export async function newEntryTicket ( name : string ) {
55
+ const body : IDictionary = {
56
+ fields : {
57
+ issuetype : {
58
+ id : config . atlassian . issueTypes . newEntry
59
+ } ,
60
+ summary : name ,
61
+ project : {
62
+ id : config . atlassian . projectId ,
63
+ }
64
+ }
65
+ }
66
+
67
+ try {
68
+ await axios . post ( config . atlassian . apiURL , body , {
69
+ auth : {
70
+ username : config . atlassian . username ,
71
+ password : config . atlassian . key ,
72
+ }
73
+ } ) ;
74
+ return true ;
75
+ } catch ( e : any ) {
76
+ console . error ( `Error while creating atlassian jira ticket! Status Code: ${ e . message } ` ) ;
77
+ return false ;
78
+ }
79
+ }
0 commit comments