-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from ymaheshwari1/#2ftb11u
Implemented: code to add functionality to subscribe or unsubscribe a webhook(#2ftb11u)
- Loading branch information
Showing
13 changed files
with
360 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import api from '@/api' | ||
|
||
const fetchShopifyWebhooks = async (payload?: any): Promise <any> => { | ||
return api({ | ||
url: "/service/getShopifyWebhooks", | ||
method: "post", | ||
data: payload | ||
}); | ||
} | ||
|
||
// TODO: add the service endpoint for the new order webhook | ||
const subscribeNewOrderWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: '', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
// TODO: add the service endpoint for the cancelled order webhook | ||
const subscribeCancelledOrderWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: '', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
// TODO: add the service endpoint for the payment status webhook | ||
const subscribePaymentStatusWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: '', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
// TODO: add the service endpoint for the order return webhook | ||
const subscribeReturnWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: '', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
// TODO: add the service endpoint for the new product webhook | ||
const subscribeNewProductsWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: '', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
const subscribeDeleteProductsWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: 'service/subscribeProductDeleteWebhook', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
const subscribeFileStatusUpdateWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: 'service/subscribeFileStatusUpdateWebhook', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
const unsubscribeWebhook = async (payload?: any): Promise <any> => { | ||
return api ({ | ||
url: 'service/removeShopifyWebhook', | ||
method: 'post', | ||
data: payload | ||
}) | ||
} | ||
|
||
export const WebhookService = { | ||
fetchShopifyWebhooks, | ||
subscribeNewOrderWebhook, | ||
subscribeCancelledOrderWebhook, | ||
subscribeFileStatusUpdateWebhook, | ||
subscribePaymentStatusWebhook, | ||
subscribeReturnWebhook, | ||
subscribeNewProductsWebhook, | ||
subscribeDeleteProductsWebhook, | ||
unsubscribeWebhook | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface WebhookState { | ||
cached: any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ActionTree } from "vuex"; | ||
import RootState from "@/store/RootState"; | ||
import WebhookState from "./WebhookState"; | ||
import { WebhookService } from "@/services/WebhookService"; | ||
import { hasError, showToast } from "@/utils"; | ||
import * as types from './mutations-types' | ||
import { translate } from '@/i18n' | ||
|
||
const actions: ActionTree<WebhookState, RootState> = { | ||
async fetchWebhooks({ commit }) { | ||
await WebhookService.fetchShopifyWebhooks({ shopifyConfigId: this.state.user.shopifyConfig }).then(resp => { | ||
if (resp.status == 200 && resp.data.webhooks?.length > 0 && !hasError(resp)) { | ||
const webhooks = resp.data.webhooks; | ||
const topics: any = {} | ||
webhooks.map((webhook: any) => { | ||
topics[webhook.topic] = webhook | ||
}) | ||
commit(types.WEBHOOK_UPDATED, topics) | ||
} | ||
}).catch(err => console.error(err)) | ||
}, | ||
async unsubscribeWebhook({ dispatch }, payload: any) { | ||
|
||
let resp; | ||
|
||
try { | ||
resp = await WebhookService.unsubscribeWebhook(payload) | ||
if (resp.status === 200 && !hasError(resp)) { | ||
showToast(translate("Webhook unsubscribed successfully")); | ||
} | ||
} catch(err) { | ||
console.log(err) | ||
showToast(translate("Something went wrong")); | ||
} finally { | ||
dispatch('fetchWebhooks') | ||
} | ||
}, | ||
async subscribeWebhook({ dispatch }, id: string) { | ||
|
||
// stores the webhook service that needs to be called on the basis of current webhook selected, doing | ||
// so as we have defined separate service for different webhook subscription | ||
const webhookMethods = { | ||
'NEW_ORDERS': WebhookService.subscribeNewOrderWebhook, | ||
'CANCELLED_ORDERS': WebhookService.subscribeCancelledOrderWebhook, | ||
'PAYMENT_STATUS':WebhookService.subscribePaymentStatusWebhook, | ||
'RETURNS': WebhookService.subscribeReturnWebhook, | ||
'NEW_PRODUCTS': WebhookService.subscribeNewProductsWebhook, | ||
'DELETE_PRODUCTS': WebhookService.subscribeDeleteProductsWebhook, | ||
'BULK_OPERATIONS_FINISH': WebhookService.subscribeFileStatusUpdateWebhook | ||
} as any | ||
const webhookMethod: any = webhookMethods[id]; | ||
|
||
if (!webhookMethod) { | ||
showToast(translate("Configuration missing")); | ||
return; | ||
} | ||
|
||
let resp; | ||
|
||
try { | ||
resp = await webhookMethod({ shopifyConfigId: this.state.user.shopifyConfig }) | ||
|
||
if (resp.status == 200 && !hasError(resp)) { | ||
showToast(translate('Webhook subscribed successfully')) | ||
} else { | ||
showToast(translate('Something went wrong')) | ||
console.error(resp) | ||
} | ||
} catch (err) { | ||
showToast(translate('Something went wrong')) | ||
console.error(err); | ||
} finally { | ||
await dispatch('fetchWebhooks') | ||
} | ||
} | ||
} | ||
|
||
export default actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { GetterTree } from "vuex"; | ||
import RootState from "@/store/RootState"; | ||
import WebhookState from "./WebhookState"; | ||
|
||
const getters: GetterTree<WebhookState, RootState> = { | ||
getCachedWebhook: (state) => state.cached | ||
} | ||
|
||
export default getters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Module } from 'vuex' | ||
import WebhookState from './WebhookState' | ||
import RootState from '@/store/RootState' | ||
import getters from './getters' | ||
import mutations from './mutations' | ||
import actions from './actions' | ||
|
||
const webhookModule: Module<WebhookState, RootState> = { | ||
namespaced: true, | ||
state: { | ||
cached: {} | ||
}, | ||
getters, | ||
actions, | ||
mutations, | ||
} | ||
|
||
export default webhookModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const SN_WEBHOOK = 'webhook' | ||
export const WEBHOOK_UPDATED = SN_WEBHOOK + '/WEBHOOK_UPDATED' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MutationTree } from "vuex"; | ||
import WebhookState from "./WebhookState"; | ||
import * as types from './mutations-types' | ||
|
||
const mutations: MutationTree<WebhookState> = { | ||
[types.WEBHOOK_UPDATED] (state, payload: any) { | ||
state.cached = payload | ||
} | ||
} | ||
|
||
export default mutations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.