1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const jira_client_1 = __importDefault(require("jira-client"));
|
16 | const tempo_client_1 = __importDefault(require("tempo-client"));
|
17 | const jira_issue_1 = __importDefault(require("./entity/jira-issue"));
|
18 | const worklog_1 = __importDefault(require("./entity/worklog"));
|
19 | const summary_item_1 = __importDefault(require("./entity/summary-item"));
|
20 | const generate_email_1 = __importDefault(require("./generate-email"));
|
21 | const user_1 = __importDefault(require("./entity/user"));
|
22 | class TempoSummaryEmail {
|
23 | constructor(options) {
|
24 | this.tempoApiKey = options.tempoApiKey;
|
25 | this.jiraUsername = options.jiraUsername;
|
26 | this.jiraApiKey = options.jiraApiKey;
|
27 | this.jiraDomain = options.jiraDomain;
|
28 | }
|
29 | generateEmailForRange(fromDate, toDate) {
|
30 | return __awaiter(this, void 0, void 0, function* () {
|
31 | const summaryItems = yield this.retrieveSummaryItems(fromDate, toDate);
|
32 | const showDates = (fromDate !== toDate);
|
33 | return (new generate_email_1.default(yield this.getUser(), summaryItems, showDates)).generateEmail();
|
34 | });
|
35 | }
|
36 | retrieveSummaryItems(from, to) {
|
37 | return __awaiter(this, void 0, void 0, function* () {
|
38 | const user = yield this.getUser();
|
39 | const tempo = this.createTempoClient();
|
40 | const jira = this.createJiraClient();
|
41 | const tempoWorklogs = yield tempo.worklogs.getForUser(user.getAccountId(), { from, to }).then((response) => { return response.results; });
|
42 | const summaryItems = {};
|
43 | for (const index in tempoWorklogs) {
|
44 | if (!Object.prototype.hasOwnProperty.call(tempoWorklogs, index)) {
|
45 | continue;
|
46 | }
|
47 | const worklog = new worklog_1.default(tempoWorklogs[index]);
|
48 | let summaryItem;
|
49 | if (worklog.getIssueKey() in summaryItems) {
|
50 | summaryItem = summaryItems[worklog.getIssueKey()];
|
51 | }
|
52 | else {
|
53 | const issue = yield jira.findIssue(worklog.getIssueKey());
|
54 | const jiraIssue = new jira_issue_1.default(issue, this.jiraDomain);
|
55 | summaryItem = new summary_item_1.default(jiraIssue);
|
56 |
|
57 | summaryItems[worklog.getIssueKey()] = summaryItem;
|
58 | }
|
59 | summaryItem.addWorklog(worklog);
|
60 | }
|
61 | return Object.keys(summaryItems).map(i => summaryItems[i]);
|
62 | });
|
63 | }
|
64 | createTempoClient() {
|
65 | return new tempo_client_1.default({
|
66 | apiVersion: "3",
|
67 | bearerToken: this.tempoApiKey,
|
68 | host: "api.tempo.io",
|
69 | protocol: "https",
|
70 | });
|
71 | }
|
72 | createJiraClient() {
|
73 | return new jira_client_1.default({
|
74 | apiVersion: "3",
|
75 | host: this.jiraDomain,
|
76 | password: this.jiraApiKey,
|
77 | protocol: "https",
|
78 | strictSSL: true,
|
79 | username: this.jiraUsername,
|
80 | });
|
81 | }
|
82 | getUser() {
|
83 | return __awaiter(this, void 0, void 0, function* () {
|
84 | if (typeof (this.user) !== "undefined") {
|
85 | return this.user;
|
86 | }
|
87 | const jira = this.createJiraClient();
|
88 | const response = yield jira.getCurrentUser().catch((reason) => {
|
89 | throw new Error(reason);
|
90 | });
|
91 | this.user = new user_1.default(response);
|
92 | return this.user;
|
93 | });
|
94 | }
|
95 | }
|
96 | exports.default = TempoSummaryEmail;
|
97 |
|
\ | No newline at end of file |