2023-05-09 10:17:54 +00:00
|
|
|
import { createLogger } from "@saleor/apps-shared";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { protectedClientProcedure } from "../trpc/protected-client-procedure";
|
|
|
|
import { router } from "../trpc/trpc-server";
|
|
|
|
import { createSettingsManager } from "./metadata-manager";
|
2023-09-07 11:59:45 +00:00
|
|
|
import { AppConfigV2 } from "./schema-v2/app-config";
|
|
|
|
import { AddressV2Schema } from "./schema-v2/app-config-schema.v2";
|
2023-05-09 10:17:54 +00:00
|
|
|
import { AppConfigV2MetadataManager } from "./schema-v2/app-config-v2-metadata-manager";
|
|
|
|
import { GetAppConfigurationV2Service } from "./schema-v2/get-app-configuration.v2.service";
|
|
|
|
|
|
|
|
const UpsertAddressSchema = z.object({
|
|
|
|
address: AddressV2Schema,
|
|
|
|
channelSlug: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const appConfigurationRouter = router({
|
|
|
|
fetchChannelsOverrides: protectedClientProcedure.query(async ({ ctx, input }) => {
|
|
|
|
const logger = createLogger({ saleorApiUrl: ctx.saleorApiUrl });
|
|
|
|
|
|
|
|
logger.debug("appConfigurationRouterV2.fetch called");
|
|
|
|
|
2023-06-27 10:13:04 +00:00
|
|
|
const appConfigV2 =
|
|
|
|
(await new GetAppConfigurationV2Service(ctx).getConfiguration()) ?? new AppConfigV2();
|
2023-05-09 10:17:54 +00:00
|
|
|
|
|
|
|
return appConfigV2.getChannelsOverrides();
|
|
|
|
}),
|
|
|
|
upsertChannelOverride: protectedClientProcedure
|
|
|
|
.meta({
|
|
|
|
requiredClientPermissions: ["MANAGE_APPS"],
|
|
|
|
})
|
|
|
|
.input(UpsertAddressSchema)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2023-06-27 10:13:04 +00:00
|
|
|
const appConfigV2 =
|
|
|
|
(await new GetAppConfigurationV2Service(ctx).getConfiguration()) ?? new AppConfigV2();
|
2023-05-09 10:17:54 +00:00
|
|
|
|
|
|
|
appConfigV2.upsertOverride(input.channelSlug, input.address);
|
|
|
|
|
|
|
|
const mm = new AppConfigV2MetadataManager(createSettingsManager(ctx.apiClient));
|
|
|
|
|
|
|
|
await mm.set(appConfigV2.serialize());
|
|
|
|
}),
|
|
|
|
removeChannelOverride: protectedClientProcedure
|
|
|
|
.meta({
|
|
|
|
requiredClientPermissions: ["MANAGE_APPS"],
|
|
|
|
})
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
channelSlug: z.string(),
|
2023-09-07 11:59:45 +00:00
|
|
|
}),
|
2023-05-09 10:17:54 +00:00
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2023-06-27 10:13:04 +00:00
|
|
|
const appConfigV2 =
|
|
|
|
(await new GetAppConfigurationV2Service(ctx).getConfiguration()) ?? new AppConfigV2();
|
2023-05-09 10:17:54 +00:00
|
|
|
|
|
|
|
appConfigV2.removeOverride(input.channelSlug);
|
|
|
|
|
|
|
|
const mm = new AppConfigV2MetadataManager(createSettingsManager(ctx.apiClient));
|
|
|
|
|
|
|
|
return mm.set(appConfigV2.serialize());
|
|
|
|
}),
|
|
|
|
});
|