var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getTyped } from 'composition';
import { oneRouter } from '@skava/router';
import ObservableContainer from 'src/bootstrapper/connectData/ObservableContainer';
import { errorContainer } from 'src/state/errorView/container';
import { isErrorLikeResponse } from '@skava/is-error-like-response';
import { LoginUserQuery, SocialLogin, RegisterGuestQuery, RegisterUserQuery, UserProfileQuery, TwitterAuthToken, UpdateSecurityQuestions, ResetBySms, ResetByEmail, ValidateByEmail, GetSecurityQuestions, UpdateProfile, ResetBySecurityQuestions, UpdatePassword, UserActivationQuery, } from './queries';
import ViewBag from '../cart/queries/ViewBag.graphql';
import { toastMessage } from 'src/state/errorView/_fixture';
import { RegisterAccountQuery } from './queries/B2B';
import CreateSavedForLater from '../cart/queries/CreateSavedForLater.graphql';
import { mapCookiesOnResponse, coerceInconsistency, keep, toStatus } from './deps';
class SessionApis extends ObservableContainer {
constructor() {
super(...arguments);
/**
* @NOTE Temporary while debugginng @michael
* @todo @FIXME @michael -- nothing is as permenant as temporary code
* -- do it properly
*/
this.registerGuestUser = () => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: RegisterGuestQuery,
variables: {},
});
const { registerGuest } = response.data;
mapCookiesOnResponse(registerGuest);
return registerGuest;
});
this.registerUser = (data, securityParams) => __awaiter(this, void 0, void 0, function* () {
const { challengeQuestion, challengeAnswer } = securityParams;
const response = yield this.client.mutate({
mutation: RegisterUserQuery,
variables: {
input: Object.assign({}, data, { challengeQuestion,
challengeAnswer }),
},
});
const { registerUser } = response.data;
mapCookiesOnResponse(registerUser);
return registerUser;
});
this.adminRegisterAccount = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: RegisterAccountQuery,
variables: {
input: data,
},
});
const { registerAccount } = response.data;
return registerAccount;
});
this.updateProfile = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: UpdateProfile,
variables: {
input: data,
},
});
const toastText = isErrorLikeResponse(response)
? toastMessage.updateProfileFailure
: toastMessage.updateProfileSuccess;
errorContainer.setError({
errorMessage: toastText,
});
const { updateProfile } = response.data;
return updateProfile;
});
this.updateSecurity = (data, isNewUser) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: UpdateSecurityQuestions,
variables: {
input: data,
},
});
const toastText = isErrorLikeResponse(response)
? toastMessage.updateQuestionFailure
: toastMessage.updateQuestionSuccess;
if (!isNewUser) {
errorContainer.setError({
errorMessage: toastText,
});
}
const { updateSecurity } = response.data;
return updateSecurity;
});
this.getSecurityQuestions = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: GetSecurityQuestions,
variables: {
input: data,
},
});
const { getSecurityQuestions } = response.data;
return getSecurityQuestions;
});
this.updatePassword = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: UpdatePassword,
variables: {
input: data,
},
});
const updatePasswordResponse = response.data.updatePassword;
if (isErrorLikeResponse(response)) {
errorContainer.setError({
errorMessage: toastMessage.updatePasswordFailure,
});
}
else {
const responseStatus = toStatus(updatePasswordResponse);
errorContainer.setError({
errorMessage: responseStatus,
});
}
// need to handle if the call had an error
// need to return error if we had error, otherwise return undefined?
return updatePasswordResponse;
});
this.resetPasswordThroughSms = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: ResetBySms,
variables: data,
});
return response.data.resetBySms;
});
this.resetPasswordThroughEmail = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: ResetByEmail,
variables: {
input: data,
},
});
return response.data.resetByEmail;
});
this.validateByEmail = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: ValidateByEmail,
variables: {
input: data,
},
});
return response.data.validateByEmail;
});
this.userActivation = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: UserActivationQuery,
variables: {
input: data,
},
});
return response.data.userActivation;
});
// @todo improve this one too
this.resetPasswordThroughSecurityQuestions = (data) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: ResetBySecurityQuestions,
variables: {
input: data,
},
});
return response.data.resetPasswordBySecurityQuestions;
});
this.logIn = (data) => __awaiter(this, void 0, void 0, function* () {
const params = coerceInconsistency(data);
console.log('[Session] login');
// @todo @@perf freeze & scope
const input = keep(params, ['password', 'userName', 'authToken', 'provider']);
const response = yield this.client.mutate({
mutation: LoginUserQuery,
variables: {
input,
},
});
return response.data.login;
});
this.makeLoginCall = (input) => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: SocialLogin,
variables: {
input,
},
});
return response.data.socialLogin;
});
// @todo cookies
this.loginWithGoogle = (authToken) => {
return this.makeLoginCall({
provider: 'google',
authToken,
});
};
// @todo cookies
this.loginWithTwitter = (authToken) => {
return this.makeLoginCall({
provider: 'twitter',
authToken,
});
};
// @todo cookies
this.loginWithFacebook = (authToken) => {
return this.makeLoginCall({
provider: 'facebook',
authToken,
});
};
this.fetchUserProfile = (shouldEnablePayment, shouldGetAddress) => __awaiter(this, void 0, void 0, function* () {
const isCheckOutOrMyAccount = oneRouter.get('pathname') === '/checkout' || oneRouter.get('pathname').includes('/myaccount');
const shouldGetPaymentFinal = isCheckOutOrMyAccount || shouldEnablePayment;
const shouldGetAddressFinal = isCheckOutOrMyAccount || shouldGetAddress;
const response = yield this.client.query({
query: UserProfileQuery,
variables: {
shouldEnablePaymentRetrival: shouldGetPaymentFinal,
shouldEnableAddressRetrival: shouldGetAddressFinal,
},
fetchPolicy: 'no-cache',
});
const { profile } = response.data;
console.log('[mike]', profile);
mapCookiesOnResponse(profile);
return profile;
});
this.createList = () => __awaiter(this, void 0, void 0, function* () {
// const response = await this.client.mutate({
// mutation: CreateLists,
// })
// return response && response.data && response.data.createList
});
this.createSaveForLater = () => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: CreateSavedForLater,
variables: {},
});
return response.data.createSaveForLater;
});
this.fetchList = () => __awaiter(this, void 0, void 0, function* () {
// const response = await this.client.query({
// query: ListsQuery,
// })
// return response.data.getLists
});
// @todo - missing gql?
this.fetchListItems = (listId) => __awaiter(this, void 0, void 0, function* () {
// const response = await this.client.query({
// // query: ListItemsQuery,
// query: FavoritesQuery,
// variables: {
// favoriteId: listId,
// // listId,
// },
// })
// return response.data.favorites
});
/**
* @see https://medium.com/@dai_shi/minimal-example-code-for-apollo-stack-7bd0cd2b7d7d
*/
this.viewBag = () => __awaiter(this, void 0, void 0, function* () {
// clearApolloCache(this.client)
// clearApolloCache({ cache })
try {
const response = yield this.client.query({
query: ViewBag,
variables: {},
// fetchPolicy: 'network-only',
// maybe causes error?????
fetchPolicy: 'no-cache',
});
return getTyped(response).obj('data.cart');
}
catch (e) {
console.log('View Bag Error On Graphql', e);
return {};
}
});
this.getTwitterAuthToken = () => __awaiter(this, void 0, void 0, function* () {
const response = yield this.client.mutate({
mutation: TwitterAuthToken,
variables: {},
});
return response.data.twitterAuthToken;
});
}
}
SessionApis.debugName = 'SessionApis';
const sessionApis = new SessionApis();
export { sessionApis };
export { SessionApis };
//# sourceMappingURL=container.apis.js.map