- Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathbulkReassignSchedules.ts
52 lines (48 loc) · 1.49 KB
/
bulkReassignSchedules.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
import{LookerNodeSDK}from'@looker/sdk-node';
import{IScheduledPlan}from'@looker/sdk';
constsdk=LookerNodeSDK.init40();
// schedules will either return for all users or for a specific user id if passed
constgetSchedules=async(userID?: number): Promise<IScheduledPlan[]>=>{
letschedules: IScheduledPlan[];
if(userID){
schedules=awaitsdk.ok(
sdk.all_scheduled_plans({user_id: userID,all_users: false})
);
}else{
schedules=awaitsdk.ok(sdk.all_scheduled_plans({all_users: true}));
}
returnschedules;
};
constbulkReAssignSchedules=async(
newOwner: number,
userID?: number
): Promise<boolean|Error>=>{
if(newOwner){
constschedules=awaitgetSchedules(userID);
try{
schedules.forEach(async(s)=>{
if(s.id){
awaitsdk.ok(sdk.update_scheduled_plan(s.id,{user_id: newOwner}));
}
});
// depending on whether userID is included or not we will change what we log to the console
console.log(
`Successfully reassigned all schedules ${
userID ? `for user ${userID}` : 'for all users'
} to ${newOwner}`
);
returntrue;
}catch(e){
thrownewError(
`There was an error trying to disable schedules. Full error message: ${e}`
);
}
}else{
thrownewError(
'Please specify at least the id of the new owner for schedules.'
);
}
};
// Examples
// bulkDisableSchedules(200)
// bulkDisableSchedules(200, 300)