saleor-apps-redis_apl/apps/taxes/scripts/migrations/app-webhook-repository.ts
Adrian Pilarczyk 416c92fb6c
feat: change the flow from OrderCreated to OrderConfirmed (#826)
* refactor: ♻️ extract order-metadata-manager

* feat: 🚧 add basic boilerplate

* feat:  add readExternalIdFromOrderMetadata

* Revert "feat:  add readExternalIdFromOrderMetadata"

This reverts commit a78d9d4597672f8605cf998a9f784aebaab27de1.

* feat:  add order-cancelled avatax adapter

* test:  add tests for AvataxOrderCancelledPayloadTransformer

* refactor: avataxId instead of externalId

* refactor: ♻️ split up webhook response

* build: ⬆️ upgrade avatax

* refactor: ♻️ extend logging in webhook response errors

* fix: 🐛 split privateMetadata and publicMetadata

* fix: 🐛 use "DEFAULT" value of companyCode for commit to work

* fix: ⚗️ fix voidTransaction type

* refactor: 🚚 order_created -> order_confirmed

* fix: 🐛 change voidReason

* build: 👷 add changeset

* refactor: 🔥 order_fulfilled webhook

* feat: Avatax metadata tax calculation date (#843)

* feat:  add metadata tax calculation date

* build: 👷 add changeset

* feat: Avatax metadata document code (#844)

* feat:  provide document code through metadata field

* build: 👷 add changeset

* refactor: ♻️ fallback to default company code for migration

* refactor: ♻️ patch order-created files and add deprecation note

* Revert "refactor: 🔥 order_fulfilled webhook"

This reverts commit fd098642735ae9d62e3a876088226bd0f108afd6.

* refactor: ♻️ patch order-fulfilled files and add deprecation note

* fix: 🐛 bring back deprecated webhooks to manifest

* feat: ⚗️ add AppWebhookMigrator (#850)

* refactor: 🚚 order_created -> order_confirmed

* refactor: 🔥 order_fulfilled webhook

* feat: ⚗️ add AppWebhookMigrator

* feat:  add mode to migrator

* feat:  add draft of run-report and migrateWebhook method

* refactor: ♻️ address feedback

* feat:  add tests and new structure

* refactor: 🔥 util

* feat:  add enable/disable webhook rollback flow

* refactor: ♻️ modify the taxes-migration flow

* refactor: ♻️ generalize document code & date resolver

* chore: 🗃️ add run-migration

* chore: 💡 update comments about migration flow

* fix: 🐛 slice document code

* refactor: ♻️ try/catch at the top level

* chore: 💡 add comments

* Update shiny-meals-wait.md

* Update soft-steaks-know.md

* Update soft-steaks-know.md

* fix:  fix test

* feat:  change createTransaction to createOrAdjustTransaction

this feature grants idempotency of the transaction flow

* feat:  add number field to OrderConfirmed payload

* chore: 💡 add deprecation comment to metadata method

* docs: 📝 add todo comment to refactor sumPayloadLines

* feat:  add resolveStringOrThrow and use it for email

* fix: 🐛 add missing number to mock
2023-08-10 13:08:20 +02:00

169 lines
3.5 KiB
TypeScript

import { createGraphQLClient } from "@saleor/apps-shared";
import { Client, gql } from "urql";
import {
CreateAppWebhookDocument,
CreateAppWebhookMutation,
CreateAppWebhookMutationVariables,
DeleteAppWebhookDocument,
DeleteAppWebhookMutation,
DeleteAppWebhookMutationVariables,
DisableWebhookDocument,
DisableWebhookMutation,
DisableWebhookMutationVariables,
EnableWebhookDocument,
EnableWebhookMutation,
EnableWebhookMutationVariables,
FetchAppWebhooksDocument,
FetchAppWebhooksQuery,
} from "../../generated/graphql";
gql`
query FetchAppWebhooks {
app {
webhooks {
id
name
}
}
}
`;
gql`
mutation CreateAppWebhook(
$appId: ID!
$name: String!
$targetUrl: String!
$query: String
$isActive: Boolean!
$asyncEvents: [WebhookEventTypeAsyncEnum!]
$syncEvents: [WebhookEventTypeSyncEnum!]
) {
webhookCreate(
input: {
app: $appId
name: $name
targetUrl: $targetUrl
query: $query
isActive: $isActive
asyncEvents: $asyncEvents
syncEvents: $syncEvents
}
) {
webhook {
id
}
}
}
`;
gql`
mutation DeleteAppWebhook($id: ID!) {
webhookDelete(id: $id) {
webhook {
id
}
}
}
`;
gql`
mutation DisableWebhook($id: ID!) {
webhookUpdate(id: $id, input: { isActive: false }) {
webhook {
id
}
}
}
`;
gql`
mutation EnableWebhook($id: ID!) {
webhookUpdate(id: $id, input: { isActive: true }) {
webhook {
id
}
}
}
`;
export class AppWebhookRepository {
constructor(private client: Client) {}
async getAll() {
const { error, data } = await this.client
.query<FetchAppWebhooksQuery>(FetchAppWebhooksDocument, {})
.toPromise();
if (error) {
console.log("Was not able to fetch app webhooks", error.message);
throw error;
}
return data?.app?.webhooks ?? [];
}
async create(variables: CreateAppWebhookMutationVariables) {
const { error, data } = await this.client
.mutation<CreateAppWebhookMutation>(CreateAppWebhookDocument, variables)
.toPromise();
if (error) {
console.log(`Was not able to create webhook for the app ${variables.appId}`, error.message);
throw error;
}
return data?.webhookCreate?.webhook?.id;
}
async disable(id: string) {
const { error, data } = await this.client
.mutation<DisableWebhookMutation>(DisableWebhookDocument, {
id,
} as DisableWebhookMutationVariables)
.toPromise();
if (error) {
console.log(`Was not able to disable webhook ${id}`, error.message);
throw error;
}
return data?.webhookUpdate?.webhook?.id;
}
async enable(id: string) {
const { error, data } = await this.client
.mutation<EnableWebhookMutation>(EnableWebhookDocument, {
id,
} as EnableWebhookMutationVariables)
.toPromise();
if (error) {
console.log(`Was not able to enable webhook ${id}`, error.message);
throw error;
}
return data?.webhookUpdate?.webhook?.id;
}
async delete(id: string) {
const { error, data } = await this.client
.mutation<DeleteAppWebhookMutation>(DeleteAppWebhookDocument, {
id,
} as DeleteAppWebhookMutationVariables)
.toPromise();
console.log(data, error);
if (error) {
console.log(`Was not able to delete webhook ${id}`, error.message);
throw error;
}
return data?.webhookDelete?.webhook?.id;
}
}