Skip to content

Commit

Permalink
3095 refactoring user vuex module
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejewsky committed Jul 18, 2019
1 parent 9c80ce0 commit 76cdcb2
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 295 deletions.
110 changes: 110 additions & 0 deletions core/data-resolver/UserService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { DataResolver } from './types/DataResolver';
import { UserProfile } from '@vue-storefront/core/modules/user/types/UserProfile'
import { TaskQueue } from '@vue-storefront/core/lib/sync'
import Task from '@vue-storefront/core/lib/sync/types/Task'
import { adjustMultistoreApiUrl } from '@vue-storefront/core/lib/multistore'
import { processURLAddress } from '@vue-storefront/core/helpers'
import config from 'config'

const getUrl = (defaultEndpoint) => processURLAddress(config.storeViews.multistore ? adjustMultistoreApiUrl(defaultEndpoint) : defaultEndpoint)

const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}

const resetPassword = async (email: string): Promise<Task> =>
TaskQueue.execute({
url: config.users.resetPassword_endpoint,
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ email })
}
})

const login = async (username: string, password: string): Promise<Task> =>
TaskQueue.execute({
url: getUrl(config.users.login_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ username, password })
}
})

const register = async (customer: DataResolver.Customer, password: string): Promise<Task> =>
TaskQueue.execute({
url: getUrl(config.users.create_endpoint),
payload: {
method: 'POST',
headers,
body: JSON.stringify({ customer, password })
}
})

const updateProfile = async (userProfile: UserProfile): Promise<Task> =>
TaskQueue.execute({
url: config.users.me_endpoint,
payload: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
mode: 'cors',
body: JSON.stringify(userProfile)
}
})

const getProfile = async () =>
TaskQueue.execute({
url: config.users.me_endpoint,
payload: {
method: 'GET',
mode: 'cors',
headers
}
})

const getOrdersHistory = async (): Promise<Task> =>
TaskQueue.execute({
url: config.users.history_endpoint,
payload: {
method: 'GET',
mode: 'cors',
headers
}
})

const changePassword = async (passwordData: DataResolver.PasswordData): Promise<Task> =>
TaskQueue.execute({
url: config.users.changePassword_endpoint,
payload: {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(passwordData)
}
})

const invalidateToken = async (refreshToken: string): Promise<Task> =>
TaskQueue.execute({
url: getUrl(config.users.refresh_endpoint),
payload: {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify({ refreshToken })
}
})

export const UserService: DataResolver.UserService = {
resetPassword,
login,
register,
updateProfile,
getProfile,
getOrdersHistory,
changePassword,
invalidateToken
}
4 changes: 3 additions & 1 deletion core/data-resolver/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CategoryService } from './CategoryService';
import { UserService } from './UserService';

export {
CategoryService
CategoryService,
UserService
}
25 changes: 25 additions & 0 deletions core/data-resolver/types/DataResolver.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Category } from 'core/modules/catalog-next/types/Category';
import { UserProfile } from 'core/modules/user/types/UserProfile'
import Task from '@vue-storefront/core/lib/sync/types/Task'

declare namespace DataResolver {

Expand All @@ -15,7 +17,30 @@ declare namespace DataResolver {
excludeFields?: string[]
}

interface Customer {
email: string,
firstname: string,
lastname: string,
addresses: string
}

interface PasswordData {
currentPassword: string,
newPassword: string
}

interface CategoryService {
getCategories: (searchRequest?: CategorySearchOptions) => Promise<Category[]>
}

interface UserService {
resetPassword: (email: string) => Promise<Task>,
login: (username: string, password: string) => Promise<Task>,
register: (customer: Customer, pssword: string) => Promise<Task>,
updateProfile: (userProfile: UserProfile) => Promise<Task>,
getProfile: () => Promise<Task>,
getOrdersHistory: () => Promise<Task>,
changePassword: (passwordData: PasswordData) => Promise<Task>,
invalidateToken: (refreshToken: string) => Promise<Task>
}
}
1 change: 1 addition & 0 deletions core/lib/sync/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
task.resultCode = jsonResponse.code
task.code = jsonResponse.code // backward compatibility to fetch()
task.acknowledged = false
task.meta = jsonResponse.meta

if (task.callback_event) {
if (task.callback_event.startsWith('store:')) {
Expand Down
3 changes: 2 additions & 1 deletion core/lib/sync/types/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default interface Task {
transmited: boolean,
transmited_at: Date,
url: string,
is_result_cacheable?: boolean
is_result_cacheable?: boolean,
meta: any
}
Loading

0 comments on commit 76cdcb2

Please sign in to comment.