-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplannerService.ts
108 lines (103 loc) · 4.1 KB
/
plannerService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Client } from "@microsoft/microsoft-graph-client";
import { createMicrosoftGraphClientWithCredential, TeamsUserCredential } from "@microsoft/teamsfx";
import { TeamsUserCredentialContext } from "../internal/singletonContext";
import { TaskAssignedToModel, TaskModel } from "../models/plannerTaskModel";
import * as configs from "../configs";
export async function getTasks(): Promise<TaskModel[]> {
let credential: TeamsUserCredential;
try {
credential = TeamsUserCredentialContext.getInstance().getCredential();
const graphClient: Client = createMicrosoftGraphClientWithCredential(credential, [
"Tasks.ReadWrite",
"Group.ReadWrite.All",
]);
const resp = await graphClient
.api(`/planner/plans/${configs.PLAN_ID}/tasks?$top=4`)
.get();
const tasksInfo = resp["value"];
let tasks: TaskModel[] = [];
for (const obj of tasksInfo) {
const taskInfo: TaskModel = {
id: obj["id"],
name: obj["title"],
priority: obj["priority"],
percentComplete: obj["percentComplete"],
};
if (obj["assignments"] !== undefined) {
let assignMap: Map<String, object> = new Map(Object.entries(obj["assignments"]));
let assignments: TaskAssignedToModel[] = [];
let overAssignments: TaskAssignedToModel[] = [];
assignMap.forEach(async (value, userId) => {
const assignInfo: TaskAssignedToModel = await getUser(userId as string);
if (assignments.length < 2) {
assignments.push(assignInfo);
} else {
overAssignments.push(assignInfo);
}
});
taskInfo.assignments = assignments;
taskInfo.overAssignments = overAssignments;
}
tasks.push(taskInfo);
await graphClient.api("/planner/tasks/" + taskInfo.id + "/details").get();
}
return tasks;
} catch (e) {
throw e;
}
}
export async function addTask(title: string): Promise<TaskModel[]> {
try {
let credential: TeamsUserCredential;
credential = TeamsUserCredentialContext.getInstance().getCredential();
const graphClient: Client = createMicrosoftGraphClientWithCredential(credential, [
"Tasks.ReadWrite",
"Group.ReadWrite.All",
]);
const plannerTask = {
planId: configs.PLAN_ID,
bucketId: configs.BUCKET_ID,
title: title,
};
await graphClient.api("/planner/tasks").post(plannerTask);
const tasks = await graphClient
.api("/planner/plans/" + configs.PLAN_ID + "/tasks?$top=4")
.get();
const tasksInfo = tasks["value"];
let taskResult: TaskModel[] = [];
for (const obj of tasksInfo) {
const tmp: TaskModel = {
id: obj["id"],
name: obj["title"],
priority: obj["priority"],
percentComplete: obj["percentComplete"],
};
taskResult.push(tmp);
}
return taskResult;
} catch (e) {
throw e;
}
}
async function getUser(userId: string): Promise<TaskAssignedToModel> {
let assignedInfo: TaskAssignedToModel = {
userId: userId,
userDisplayName: "",
userAvatar: undefined,
};
try {
let credential: TeamsUserCredential;
credential = TeamsUserCredentialContext.getInstance().getCredential();
const graphClient: Client = createMicrosoftGraphClientWithCredential(credential, [
"User.Read.All",
]);
const userInfo = await graphClient.api(`/users/${userId}`).get();
assignedInfo.userDisplayName = userInfo["displayName"];
const photo = await graphClient.api(`/users/${userId}/photo/$value`).get();
assignedInfo.userAvatar = photo;
return assignedInfo;
} catch (e) {
console.log("getUser error: " + e);
}
return assignedInfo;
}