saleor-apps-redis_apl/apps/emails-and-messages/src/modules/app-configuration/app-configuration.router.ts

67 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-03-06 13:01:03 +00:00
import { logger as pinoLogger } from "../../lib/logger";
2023-03-07 21:02:37 +00:00
import {
appChannelConfigurationInputSchema,
appConfigInputSchema,
} from "./app-config-input-schema";
import { AppConfigurationService } from "./get-app-configuration.service";
2023-03-06 13:01:03 +00:00
import { router } from "../trpc/trpc-server";
import { protectedClientProcedure } from "../trpc/protected-client-procedure";
2023-03-07 21:02:37 +00:00
import { z } from "zod";
// Allow access only for the dashboard users and attaches the
// configuration service to the context
const protectedWithConfigurationService = protectedClientProcedure.use(({ next, ctx }) =>
next({
ctx: {
...ctx,
configurationService: new AppConfigurationService({
apiClient: ctx.apiClient,
saleorApiUrl: ctx.saleorApiUrl,
}),
},
})
);
2023-03-06 13:01:03 +00:00
export const appConfigurationRouter = router({
2023-03-07 21:02:37 +00:00
getChannelConfiguration: protectedWithConfigurationService
.input(z.object({ channelSlug: z.string() }))
.query(async ({ ctx, input }) => {
const logger = pinoLogger.child({ saleorApiUrl: ctx.saleorApiUrl });
logger.debug("Get Channel Configuration called");
return await ctx.configurationService.getChannelConfiguration(input.channelSlug);
}),
setChannelConfiguration: protectedWithConfigurationService
.meta({ requiredClientPermissions: ["MANAGE_APPS"] })
.input(appChannelConfigurationInputSchema)
.mutation(async ({ ctx, input }) => {
const logger = pinoLogger.child({ saleorApiUrl: ctx.saleorApiUrl });
logger.debug("Set channel configuration called");
await ctx.configurationService.setChannelConfiguration(input);
}),
fetch: protectedWithConfigurationService.query(async ({ ctx, input }) => {
2023-03-06 13:01:03 +00:00
const logger = pinoLogger.child({ saleorApiUrl: ctx.saleorApiUrl });
logger.debug("appConfigurationRouter.fetch called");
2023-03-07 21:02:37 +00:00
return new AppConfigurationService({
2023-03-06 13:01:03 +00:00
apiClient: ctx.apiClient,
saleorApiUrl: ctx.saleorApiUrl,
}).getConfiguration();
}),
2023-03-07 21:02:37 +00:00
setAndReplace: protectedWithConfigurationService
2023-03-06 13:01:03 +00:00
.meta({ requiredClientPermissions: ["MANAGE_APPS"] })
.input(appConfigInputSchema)
.mutation(async ({ ctx, input }) => {
const logger = pinoLogger.child({ saleorApiUrl: ctx.saleorApiUrl });
logger.debug(input, "appConfigurationRouter.setAndReplace called with input");
2023-03-07 21:02:37 +00:00
await ctx.configurationService.setConfigurationRoot(input);
2023-03-06 13:01:03 +00:00
return null;
}),
});