saleor-apps-redis_apl/apps/taxes/src/modules/avatax/avatax-connection.router.ts
Adrian Pilarczyk d42c79f366
fix: tax code matcher QA (#680)
* feat:  bring back validateAddress in TaxJar

* feat: 💄 decrease marginLeft of AppToggle label

* refactor: 🚚 cancelButton -> leftButton

* feat: 🧱 add data-testid to all buttons and forms

* refactor: ♻️ refactor app-section to accept Box props

* feat: 🧱 add rest of data-testid

* feat:  verify connections before displaying matcher pages

* feat:  always display matcher-section

* refactor: ♻️ improve fetching tax codes by adding retry and redirect on error

* refactor: 🚚 active-connection -> get-active-connection-service && improve logs

* chore: 🔊 remove objects from logs

* docs: 📝 add TESTING.md with edge-cases
2023-06-28 15:03:24 +02:00

112 lines
3.1 KiB
TypeScript

import { z } from "zod";
import { createLogger } from "../../lib/logger";
import { protectedClientProcedure } from "../trpc/protected-client-procedure";
import { router } from "../trpc/trpc-server";
import { avataxConfigSchema } from "./avatax-connection-schema";
import { PublicAvataxConnectionService } from "./configuration/public-avatax-connection.service";
const getInputSchema = z.object({
id: z.string(),
});
const deleteInputSchema = z.object({
id: z.string(),
});
const patchInputSchema = z.object({
id: z.string(),
value: avataxConfigSchema.deepPartial(),
});
const postInputSchema = z.object({
value: avataxConfigSchema,
});
const protectedWithConfigurationService = protectedClientProcedure.use(({ next, ctx }) =>
next({
ctx: {
connectionService: new PublicAvataxConnectionService(
ctx.apiClient,
ctx.appId!,
ctx.saleorApiUrl
),
},
})
);
export const avataxConnectionRouter = router({
verifyConnections: protectedWithConfigurationService.query(async ({ ctx }) => {
const logger = createLogger({
name: "avataxConnectionRouter.verifyConnections",
});
logger.debug("Route verifyConnections called");
await ctx.connectionService.verifyConnections();
logger.info("Avatax connections were successfully verified");
return { ok: true };
}),
getById: protectedWithConfigurationService.input(getInputSchema).query(async ({ ctx, input }) => {
const logger = createLogger({
name: "avataxConnectionRouter.get",
});
logger.debug("Route get called");
const result = await ctx.connectionService.getById(input.id);
logger.info(`Avatax configuration with an id: ${result.id} was successfully retrieved`);
return result;
}),
create: protectedWithConfigurationService
.input(postInputSchema)
.mutation(async ({ ctx, input }) => {
const logger = createLogger({
saleorApiUrl: ctx.saleorApiUrl,
procedure: "avataxConnectionRouter.post",
});
logger.debug("Attempting to create configuration");
const result = await ctx.connectionService.create(input.value);
logger.info("Avatax configuration was successfully created");
return result;
}),
delete: protectedWithConfigurationService
.input(deleteInputSchema)
.mutation(async ({ ctx, input }) => {
const logger = createLogger({
saleorApiUrl: ctx.saleorApiUrl,
procedure: "avataxConnectionRouter.delete",
});
logger.debug("Route delete called");
const result = await ctx.connectionService.delete(input.id);
logger.info(`Avatax configuration with an id: ${input.id} was deleted`);
return result;
}),
update: protectedWithConfigurationService
.input(patchInputSchema)
.mutation(async ({ ctx, input }) => {
const logger = createLogger({
saleorApiUrl: ctx.saleorApiUrl,
procedure: "avataxConnectionRouter.patch",
});
logger.debug("Route patch called");
const result = await ctx.connectionService.update(input.id, input.value);
logger.info(`Avatax configuration with an id: ${input.id} was successfully updated`);
return result;
}),
});