refactor: ♻️ move address from channel to provider

This commit is contained in:
Adrian Pilarczyk 2023-04-28 10:24:45 +02:00
parent 3a22d0af45
commit 25151ca5cd
8 changed files with 77 additions and 49 deletions

View file

@ -20,6 +20,13 @@ const mockedProviders: ProvidersConfig = [
password: "avatax-password",
username: "avatax-username",
},
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
},
},
{
@ -31,13 +38,6 @@ const mockedProviders: ProvidersConfig = [
credentials: {
apiKey: "taxjar-api-key",
},
},
},
];
const mockedEncryptedProviders = encrypt(JSON.stringify(mockedProviders), mockedSecretKey);
const mockedChannels: ChannelsConfig = {
"default-channel": {
address: {
city: "New York",
country: "US",
@ -45,6 +45,13 @@ const mockedChannels: ChannelsConfig = {
street: "123 Main St",
zip: "10001",
},
},
},
];
const mockedEncryptedProviders = encrypt(JSON.stringify(mockedProviders), mockedSecretKey);
const mockedChannels: ChannelsConfig = {
"default-channel": {
providerInstanceId: "1",
},
};

View file

@ -1,8 +1,8 @@
import { AddressLocationInfo as AvataxAddress } from "avatax/lib/models/AddressLocationInfo";
import { ChannelAddress } from "../channels-configuration/channels-config";
import { AddressFragment as SaleorAddress } from "../../../generated/graphql";
import { AvataxConfig } from "./avatax-config";
import { AddressFragment } from "../../../generated/graphql";
function mapSaleorAddressToAvataxAddress(address: SaleorAddress): AvataxAddress {
function mapSaleorAddressToAvataxAddress(address: AddressFragment): AvataxAddress {
return {
line1: address.streetAddress1,
line2: address.streetAddress2,
@ -13,7 +13,7 @@ function mapSaleorAddressToAvataxAddress(address: SaleorAddress): AvataxAddress
};
}
function mapChannelAddressToAvataxAddress(address: ChannelAddress): AvataxAddress {
function mapChannelAddressToAvataxAddress(address: AvataxConfig["address"]): AvataxAddress {
return {
line1: address.street,
city: address.city,

View file

@ -1,7 +1,15 @@
import { z } from "zod";
import { obfuscateSecret } from "../../lib/utils";
const avataxCredentials = z.object({
const addressSchema = z.object({
country: z.string(),
zip: z.string(),
state: z.string(),
city: z.string(),
street: z.string(),
});
const avataxCredentialsSchema = z.object({
username: z.string().min(1, { message: "Username requires at least one character." }),
password: z.string().min(1, { message: "Password requires at least one character." }),
});
@ -12,7 +20,8 @@ export const avataxConfigSchema = z.object({
companyCode: z.string().optional(),
isAutocommit: z.boolean(),
shippingTaxCode: z.string().optional(),
credentials: avataxCredentials,
credentials: avataxCredentialsSchema,
address: addressSchema,
});
export type AvataxConfig = z.infer<typeof avataxConfigSchema>;
@ -27,6 +36,13 @@ export const defaultAvataxConfig: AvataxConfig = {
username: "",
password: "",
},
address: {
city: "",
country: "",
state: "",
street: "",
zip: "",
},
};
export const avataxInstanceConfigSchema = z.object({

View file

@ -1,19 +1,8 @@
import { z } from "zod";
import { ChannelFragment } from "../../../generated/graphql";
const addressSchema = z.object({
country: z.string(),
zip: z.string(),
state: z.string(),
city: z.string(),
street: z.string(),
});
export type ChannelAddress = z.infer<typeof addressSchema>;
export const channelSchema = z.object({
providerInstanceId: z.string(),
address: addressSchema,
});
export type ChannelConfig = z.infer<typeof channelSchema>;
@ -22,13 +11,6 @@ export type ChannelsConfig = z.infer<typeof channelsSchema>;
export const defaultChannelConfig: ChannelConfig = {
providerInstanceId: "",
address: {
city: "",
country: "",
state: "",
street: "",
zip: "",
},
};
export const createDefaultChannelsConfig = (channels: ChannelFragment[]): ChannelsConfig => {

View file

@ -29,6 +29,13 @@ const mockedProviders: ProvidersConfig = [
password: "avatax-password",
username: "avatax-username",
},
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
},
},
{
@ -40,6 +47,13 @@ const mockedProviders: ProvidersConfig = [
credentials: {
apiKey: "taxjar-api-key",
},
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
},
},
];
@ -47,26 +61,12 @@ const mockedEncryptedProviders = encrypt(JSON.stringify(mockedProviders), mocked
const mockedChannelsWithInvalidProviderInstanceId: ChannelsConfig = {
"default-channel": {
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
providerInstanceId: "3",
},
};
const mockedValidChannels: ChannelsConfig = {
"default-channel": {
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
providerInstanceId: "1",
},
};

View file

@ -1,23 +1,39 @@
import { z } from "zod";
import { obfuscateSecret } from "../../lib/utils";
const credentials = z.object({
const addressSchema = z.object({
country: z.string(),
zip: z.string(),
state: z.string(),
city: z.string(),
street: z.string(),
});
const taxJarCredentialsSchema = z.object({
apiKey: z.string().min(1, { message: "API Key requires at least one character." }),
});
export const taxJarConfigSchema = z.object({
name: z.string().min(1, { message: "Name requires at least one character." }),
isSandbox: z.boolean(),
credentials,
credentials: taxJarCredentialsSchema,
address: addressSchema,
});
export type TaxJarConfig = z.infer<typeof taxJarConfigSchema>;
export const defaultTaxJarConfig: TaxJarConfig = {
name: "",
isSandbox: false,
credentials: {
apiKey: "",
},
isSandbox: false,
address: {
city: "",
country: "",
state: "",
street: "",
zip: "",
},
};
export const taxJarInstanceConfigSchema = z.object({

View file

@ -9,8 +9,8 @@ import { TaxJarOrderCreatedAdapter } from "./order-created/taxjar-order-created-
export class TaxJarWebhookService implements ProviderWebhookService {
client: TaxJarClient;
config: TaxJarConfig;
private logger: Logger;
private config: TaxJarConfig;
constructor(config: TaxJarConfig) {
const taxJarClient = new TaxJarClient(config);

View file

@ -39,6 +39,13 @@ const MOCKED_PROVIDERS: ProvidersConfig = [
credentials: {
apiKey: "1234",
},
address: {
city: "New York",
country: "US",
state: "NY",
street: "123 Main St",
zip: "10001",
},
},
id: "1",
},