74fc5eb6a5338025967f71cba5cfe100a7e768c9
[arvados-workbench2.git] / src / services / link-account-service / link-account-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { AxiosInstance } from "axios";
6 import { ApiActions } from "~/services/api/api-actions";
7 import { AccountToLink } from "~/models/link-account";
8 import { CommonService } from "~/services/common-service/common-service";
9
10 export const USER_LINK_ACCOUNT_KEY = 'accountToLink';
11
12 export class LinkAccountService {
13
14     constructor(
15         protected serverApi: AxiosInstance,
16         protected actions: ApiActions) { }
17
18     public saveToSession(account: AccountToLink) {
19         sessionStorage.setItem(USER_LINK_ACCOUNT_KEY, JSON.stringify(account));
20     }
21
22     public removeFromSession() {
23         sessionStorage.removeItem(USER_LINK_ACCOUNT_KEY);
24     }
25
26     public getFromSession() {
27         const data = sessionStorage.getItem(USER_LINK_ACCOUNT_KEY);
28         return data ? JSON.parse(data) as AccountToLink : undefined;
29     }
30
31     public linkAccounts(newUserToken: string, newGroupUuid: string) {
32         const params = {
33             new_user_token: newUserToken,
34             new_owner_uuid: newGroupUuid,
35             redirect_to_new_user: true
36         };
37         return CommonService.defaultResponse(
38             this.serverApi.post('/users/merge/', params),
39             this.actions,
40             false
41         );
42     }
43 }