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';
|
2019-07-18 14:39:39 +01:00
|
|
|
import { UpdatedUser, User } from '@/types/users';
|
|
|
|
import { RequestResponse } from '@/types/response';
|
2018-11-30 15:49:14 +00:00
|
|
|
|
|
|
|
// Performs update user info graphQL mutation request.
|
|
|
|
// Returns User object if succeed, null otherwise
|
2019-04-25 16:06:19 +01:00
|
|
|
export async function updateAccountRequest(user: UpdatedUser): Promise<RequestResponse<User>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<User> = new RequestResponse<User>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apolloManager.mutate(
|
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
|
|
|
updateAccount (
|
|
|
|
input: {
|
|
|
|
fullName: "${user.fullName}",
|
|
|
|
shortName: "${user.shortName}"
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
) {
|
|
|
|
email,
|
|
|
|
fullName,
|
|
|
|
shortName
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01: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
|
|
|
}
|
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>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<null> = new RequestResponse<null>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apolloManager.mutate(
|
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
|
|
|
changePassword (
|
|
|
|
password: "${password}",
|
|
|
|
newPassword: "${newPassword}"
|
|
|
|
) {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
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
|
|
|
}
|
|
|
|
|
2019-04-10 20:16:10 +01:00
|
|
|
export async function forgotPasswordRequest(email: string): Promise<RequestResponse<null>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<null> = new RequestResponse<null>();
|
2019-04-10 20:16:10 +01:00
|
|
|
|
|
|
|
let response: any = await apolloManager.query(
|
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
forgotPassword(email: "${email}")
|
|
|
|
}`),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:49:14 +00:00
|
|
|
// Performs Create user graqhQL request.
|
2019-07-23 17:08:07 +01:00
|
|
|
export async function createUserRequest(user: User, password: string, secret: string, refUserId?: string): Promise<RequestResponse<string>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<string> = new RequestResponse<string>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response = await apolloManager.mutate(
|
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
input:{
|
|
|
|
email: "${user.email}",
|
|
|
|
password: "${password}",
|
|
|
|
fullName: "${user.fullName}",
|
|
|
|
shortName: "${user.shortName}",
|
2019-07-19 19:22:10 +01:00
|
|
|
partnerId: "${user.partnerId}",
|
2019-04-05 16:08:14 +01:00
|
|
|
},
|
2019-07-23 17:08:07 +01:00
|
|
|
refUserId: "${refUserId || ""}",
|
2019-04-05 16:08:14 +01:00
|
|
|
secret: "${secret}",
|
2019-05-15 09:28:36 +01:00
|
|
|
){email, id}
|
2019-04-05 16:08:14 +01:00
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
2019-05-15 09:28:36 +01:00
|
|
|
if (response.data) {
|
|
|
|
result.data = response.data.createUser.id;
|
|
|
|
}
|
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.
|
2019-04-05 16:08:14 +01:00
|
|
|
// Returns Token.
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function getTokenRequest(email: string, password: string): Promise<RequestResponse<string>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<string> = new RequestResponse<string>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apolloManager.query(
|
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
token(email: "${email}",
|
|
|
|
password: "${password}") {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01: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
|
|
|
}
|
|
|
|
|
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.
|
2019-04-05 16:08:14 +01:00
|
|
|
// Returns User object.
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function getUserRequest(): Promise<RequestResponse<User>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<User> = new RequestResponse<User>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apolloManager.query(
|
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
user {
|
|
|
|
fullName,
|
|
|
|
shortName,
|
|
|
|
email,
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01: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
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return result;
|
2018-11-30 15:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performs graqhQL request.
|
2018-12-24 12:52:52 +00:00
|
|
|
export async function deleteAccountRequest(password: string): Promise<RequestResponse<null>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<null> = new RequestResponse<null>();
|
2018-12-20 16:18:08 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response = await apolloManager.mutate(
|
|
|
|
{
|
|
|
|
mutation: gql(`
|
|
|
|
mutation {
|
|
|
|
deleteAccount(password: "${password}") {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2018-12-24 12:52:52 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
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
|
|
|
}
|
2019-05-15 09:28:36 +01:00
|
|
|
|
|
|
|
export async function resendEmailRequest(userID: string): Promise<RequestResponse<null>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<null> = new RequestResponse<null>();
|
2019-05-15 09:28:36 +01:00
|
|
|
|
|
|
|
let response = await apolloManager.query(
|
|
|
|
{
|
|
|
|
query: gql(`
|
|
|
|
query {
|
|
|
|
resendAccountActivationEmail(id: "${userID}")
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|