2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-30 15:49:14 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-18 14:43:23 +00:00
|
|
|
import apolloManager from '../utils/apolloManager';
|
|
|
|
import gql from 'graphql-tag';
|
2018-11-30 15:49:14 +00:00
|
|
|
|
|
|
|
// Performs update user info graphQL mutation request.
|
|
|
|
// Returns User object if succeed, null otherwise
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function updateAccountRequest(user: User): Promise<RequestResponse<User>> {
|
|
|
|
let result: RequestResponse<User> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response: any = await apolloManager.mutate(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
2018-12-24 12:52:52 +00:00
|
|
|
updateAccount (
|
2018-12-20 16:18:08 +00:00
|
|
|
input: {
|
|
|
|
email: "${user.email}",
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: "${user.fullName}",
|
|
|
|
shortName: "${user.shortName}"
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
email,
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName,
|
|
|
|
shortName
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
result.data = response.data.updateAccount;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
2018-11-30 15:49:14 +00:00
|
|
|
|
2018-12-26 15:05:33 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs change password graphQL mutation
|
|
|
|
// Returns base user fields
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function changePasswordRequest(password: string, newPassword: string): Promise<RequestResponse<null>> {
|
|
|
|
let result: RequestResponse<null> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response: any = await apolloManager.mutate(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
2018-12-24 12:52:52 +00:00
|
|
|
changePassword (
|
|
|
|
password: "${password}",
|
|
|
|
newPassword: "${newPassword}"
|
2018-12-20 16:18:08 +00:00
|
|
|
) {
|
2018-12-24 12:52:52 +00:00
|
|
|
email
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
2018-11-30 15:49:14 +00:00
|
|
|
|
2018-12-26 15:05:33 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs Create user graqhQL request.
|
|
|
|
// Throws an exception if error occurs
|
|
|
|
// Returns object with newly created user
|
2019-03-20 16:16:30 +00:00
|
|
|
export async function createUserRequest(user: User, password: string, secret: string): Promise<RequestResponse<null>> {
|
2018-12-24 12:52:52 +00:00
|
|
|
let result: RequestResponse<null> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response = await apolloManager.mutate(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
input:{
|
|
|
|
email: "${user.email}",
|
|
|
|
password: "${password}",
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: "${user.fullName}",
|
|
|
|
shortName: "${user.shortName}",
|
2019-03-20 16:16:30 +00:00
|
|
|
},
|
|
|
|
secret: "${secret}",
|
2019-02-11 10:33:56 +00:00
|
|
|
){email}
|
2018-12-20 16:18:08 +00:00
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
2018-11-30 15:49:14 +00:00
|
|
|
|
2018-12-26 15:05:33 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs graqhQL request.
|
|
|
|
// Returns Token, User objects.
|
|
|
|
// Throws an exception if error occurs
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function getTokenRequest(email: string, password: string): Promise<RequestResponse<string>> {
|
|
|
|
let result: RequestResponse<string> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: ''
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response: any = await apolloManager.query(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
token(email: "${email}",
|
|
|
|
password: "${password}") {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
result.data = response.data.token.token;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return result;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
2018-11-30 15:49:14 +00:00
|
|
|
|
2018-12-20 16:18:08 +00:00
|
|
|
// Performs graqhQL request.
|
|
|
|
// Returns Token, User objects.
|
|
|
|
// Throws an exception if error occurs
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function getUserRequest(): Promise<RequestResponse<User>> {
|
|
|
|
let result: RequestResponse<User> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName: '',
|
|
|
|
shortName: '',
|
2019-03-20 16:16:30 +00:00
|
|
|
email: '',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response: any = await apolloManager.query(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
user {
|
2019-03-27 12:33:32 +00:00
|
|
|
fullName,
|
|
|
|
shortName,
|
2018-12-20 16:18:08 +00:00
|
|
|
email,
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
result.data = response.data.user;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs graqhQL request.
|
|
|
|
// User object.
|
|
|
|
// Throws an exception if error occurs
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function deleteAccountRequest(password: string): Promise<RequestResponse<null>> {
|
|
|
|
let result: RequestResponse<null> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
2018-12-20 16:18:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-24 12:52:52 +00:00
|
|
|
let response = await apolloManager.mutate(
|
2018-12-20 16:18:08 +00:00
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
2018-12-24 12:52:52 +00:00
|
|
|
deleteAccount(password: "${password}") {
|
|
|
|
email
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache'
|
|
|
|
}
|
|
|
|
);
|
2018-12-24 12:52:52 +00:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
}
|
2018-12-20 16:18:08 +00:00
|
|
|
} catch (e) {
|
2018-12-24 12:52:52 +00:00
|
|
|
result.errorMessage = e.message;
|
2018-12-20 16:18:08 +00:00
|
|
|
}
|
2018-11-30 15:49:14 +00:00
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|