
* refactor: 🔊 improve logging in taxes * refactor: 🔥 redundant channels call * refactor: ♻️ get app config from payload * build: add changeset * refactor: 🔊 routers info -> debug * refactor: 🔊 redact logs
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { decrypt } from "@saleor/app-sdk/settings-manager";
|
|
import { ExpectedWebhookPayload } from "../../lib/saleor/schema";
|
|
import { ChannelsConfig, channelsSchema } from "../channels-configuration/channels-config";
|
|
import { ProvidersConfig, providersSchema } from "../providers-configuration/providers-config";
|
|
|
|
export const getAppConfig = (payload: ExpectedWebhookPayload) => {
|
|
const metadata = payload.recipient?.privateMetadata;
|
|
let providersConfig = [] as ProvidersConfig;
|
|
let channelsConfig = {} as ChannelsConfig;
|
|
|
|
const secretKey = process.env.SECRET_KEY;
|
|
|
|
if (!secretKey) {
|
|
throw new Error("SECRET_KEY env variable is not set");
|
|
}
|
|
|
|
// * The App Config contains two types of data: providers and channels.
|
|
// * We must recognize which one we are dealing with and parse it accordingly.
|
|
metadata?.forEach((item) => {
|
|
const decrypted = decrypt(item.value, secretKey);
|
|
const parsed = JSON.parse(decrypted);
|
|
|
|
const providersValidation = providersSchema.safeParse(parsed);
|
|
|
|
if (providersValidation.success) {
|
|
providersConfig = providersValidation.data;
|
|
return;
|
|
}
|
|
|
|
const channelsValidation = channelsSchema.safeParse(parsed);
|
|
|
|
if (channelsValidation.success) {
|
|
channelsConfig = channelsValidation.data;
|
|
return;
|
|
}
|
|
});
|
|
|
|
return { providers: providersConfig, channels: channelsConfig };
|
|
};
|