Skip to content

Commit

Permalink
[skip-ci] add comments marking usage of metadata in avatax
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Aug 19, 2024
1 parent b5433cb commit 96629f7
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/avatax/graphql/fragments/CalculateTaxesEvent.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fragment CalculateTaxesEvent on Event {
...TaxBase
}
recipient {
# Entire metadata is fetched to provide app configuration. This can be moved to DB
privateMetadata {
key
value
Expand Down
4 changes: 4 additions & 0 deletions apps/avatax/graphql/fragments/TaxBase.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ fragment TaxBase on TaxableObject {
__typename
... on Checkout {
id
# TODO what was that
avataxEntityCode: metafield(key: "avataxEntityCode")
# Legacy field - moved to user
avataxCustomerCode: metafield(key: "avataxCustomerCode")
user {
...User
}
}
... on Order {
id
# TODO what was that
avataxEntityCode: metafield(key: "avataxEntityCode")
# Legacy field - moved to user
avataxCustomerCode: metafield(key: "avataxCustomerCode")
user {
...User
Expand Down
1 change: 1 addition & 0 deletions apps/avatax/graphql/fragments/User.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fragment User on User {
id
email
# Metadata is fetched with user. This is on purpose - must be set on the frontend.
avataxCustomerCode: metafield(key: "avataxCustomerCode")
}
1 change: 1 addition & 0 deletions apps/avatax/graphql/queries/FetchAppDetails.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query FetchAppDetails {
app {
id
# Programmatic fetch of metadata -> to fetch config
privateMetadata {
key
value
Expand Down
1 change: 1 addition & 0 deletions apps/avatax/graphql/queries/FetchAppMetafields.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query FetchAppMetafields($keys: [String!]) {
app {
id
# Programmatic fetch of metadata -> to fetch config
privateMetafields(keys: $keys)
}
}
1 change: 1 addition & 0 deletions apps/avatax/graphql/subscriptions/OrderCancelled.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fragment OrderCancelledSubscription on Order {
id
# Reference to Avatax transaction. Can be stored in the DB?
avataxId: metafield(key: "avataxId")
channel {
id
Expand Down
4 changes: 4 additions & 0 deletions apps/avatax/saleor-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ switch (process.env.APL) {
throw new Error("Rest APL is not configured - missing env variables. Check saleor-app.ts");
}

/**
* APL:
* - Call REST service every time request to app is executed (webhook, frontend call)
*/
apl = new SaleorCloudAPL({
resourceUrl: process.env.REST_APL_ENDPOINT,
token: process.env.REST_APL_TOKEN,
Expand Down
3 changes: 3 additions & 0 deletions apps/avatax/src/lib/app-config-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export interface IAppConfigExtractor {

/**
* Extracts app configuration from metadata. Performs initial validation, shared by all clients
*
* It takes metadata from event (or programmatic call) and maps it to the object.
* This can be replaced by similar entity, calling DB
*/
export class AppConfigExtractor implements IAppConfigExtractor {
static AppConfigExtractorError = BaseError.subclass("AppConfigExtractorError");
Expand Down
2 changes: 2 additions & 0 deletions apps/avatax/src/modules/app/order-metadata-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class OrderMetadataManager {
* @param orderId - Saleor order id
* @param externalId - Provider order id
* @deprecated - This will not be needed when we move to the new webhook flow because the transactions will be commited during OrderConfirmed
*
* Do we need to store this value? It seems like its stored to cancel order in order-cancelled. So we can store this in DB
*/
async updateOrderMetadataWithExternalId(orderId: string, externalId: string) {
const variables: UpdatePublicMetadataMutationVariables = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

const getSchema = avataxConnectionSchema.strict();

// Uses metadata to CRUD settings
export class AvataxConnectionRepository {
private crudSettingsManager: CrudSettingsManager;
private logger = createLogger("AvataxConnectionRepository", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class AvataxPatchInputTransformer {
}

async patchCredentials(id: string, input: AvataxConfig["credentials"]) {
// Metadata call
const connection = await this.connection.getById(id);

const credentials: AvataxConfig["credentials"] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const getAllForIdSchema = z.object({
uniqueKey: z.string(),
});

// TODO: Add test, but create dependency injection first, so services can be injected in ctx
/*
* TODO: Add test, but create dependency injection first, so services can be injected in ctx
* Client router - only CRUD from frontend handled here
*/
export const avataxTaxCodesRouter = router({
getAllForId: protectedClientProcedure.input(getAllForIdSchema).query(async ({ ctx, input }) => {
const logger = createLogger("avataxTaxCodesRouter.getAllForId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createLogger } from "../../logger";
const settingSchema = z.record(z.any()).and(z.object({ id: z.string() }));
const settingsSchema = z.array(settingSchema);

// Abstraction on CRUD operations - writing to Saleor metadata
export class CrudSettingsManager {
private logger = createLogger("CrudSettingsManager");

Expand Down
2 changes: 2 additions & 0 deletions apps/avatax/src/pages/api/webhooks/order-confirmed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default wrapWithLoggerContext(
.json({ message: `Order ${payload.order?.id} has flat rates tax strategy.` });
}

// metadata passed through subscription payload
const appMetadata = payload.recipient?.privateMetadata ?? [];

const configExtractor = new AppConfigExtractor();
Expand Down Expand Up @@ -155,6 +156,7 @@ export default wrapWithLoggerContext(

const orderMetadataManager = new OrderMetadataManager(client);

// We run metadata mutation here. Its public metadata - result from Avatax attached to Saleor entity
await orderMetadataManager.updateOrderMetadataWithExternalId(
confirmedOrderEvent.getOrderId(),
confirmedOrder.id,
Expand Down

0 comments on commit 96629f7

Please sign in to comment.