diff --git a/manifest.json b/manifest.json index 13ab764..47096a9 100644 --- a/manifest.json +++ b/manifest.json @@ -121,6 +121,12 @@ "host": "rc.vtex.com", "path": "/api/analytics/schemaless-events" } + }, + { + "name": "colossus-fire-event" + }, + { + "name": "colossus-write-logs" } ], "settingsSchema": { diff --git a/node/config/setupAppConfiguration.ts b/node/config/setupAppConfiguration.ts new file mode 100644 index 0000000..753e9d0 --- /dev/null +++ b/node/config/setupAppConfiguration.ts @@ -0,0 +1,24 @@ +import { setupAppSettings } from '../resolvers/Queries/Settings' + +const setupAppConfiguration = async ( + ctx: InstalledAppEvent, + next: () => Promise +) => { + ctx.vtex.logger.warn('########## ---- setupAppConfiguration ---- ##########') + + try { + await setupAppSettings(ctx as unknown as Context) + } catch (error) { + ctx.vtex.logger.error({ + setupAppConfigurationError: { + status: 'failed', + content: error, + }, + }) + throw new Error("Couldn't setup app settings") + } + + await next() +} + +export default setupAppConfiguration diff --git a/node/index.ts b/node/index.ts index c8fd587..ce8d85f 100644 --- a/node/index.ts +++ b/node/index.ts @@ -1,17 +1,18 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { ClientsConfig, - ServiceContext, - ParamsContext, - RecorderState, EventContext, IOContext, + ParamsContext, + RecorderState, SegmentData, + ServiceContext, } from '@vtex/api' -import { method, Service, AuthType, LRUCache } from '@vtex/api' +import { AuthType, LRUCache, method, Service } from '@vtex/api' -import { schemaDirectives } from './directives' import { Clients } from './clients' +import setupAppConfiguration from './config/setupAppConfiguration' +import { schemaDirectives } from './directives' import { resolvers } from './resolvers' const TIMEOUT_MS = 5000 @@ -63,10 +64,19 @@ declare global { interface State { code: number } + + interface InstalledAppEvent extends EventContext { + body: { id?: string } + } } export default new Service({ clients, + events: { + onAppInstalled: setupAppConfiguration, + onAppLinked: setupAppConfiguration, + onSettingsChanged: setupAppConfiguration, + }, graphql: { resolvers: { Mutation: resolvers.Mutation, diff --git a/node/resolvers/Queries/Settings.ts b/node/resolvers/Queries/Settings.ts index 7da5ba1..c56cc67 100644 --- a/node/resolvers/Queries/Settings.ts +++ b/node/resolvers/Queries/Settings.ts @@ -10,7 +10,7 @@ export const getAppId = (): string => { return appName } -export const getAppSettings = async (_: any, __: any, ctx: Context) => { +export async function setupAppSettings(ctx: Context) { const { clients: { masterdata, vbase }, vtex: { logger }, @@ -83,6 +83,10 @@ export const getAppSettings = async (_: any, __: any, ctx: Context) => { return settings } +export const getAppSettings = async (_: any, __: any, ctx: Context) => { + return setupAppSettings(ctx) +} + export const getSessionWatcher = async (_: any, __: any, ctx: Context) => { const { clients: { vbase }, diff --git a/node/resolvers/Routes/index.ts b/node/resolvers/Routes/index.ts index 3964768..1e977c6 100644 --- a/node/resolvers/Routes/index.ts +++ b/node/resolvers/Routes/index.ts @@ -1,11 +1,11 @@ import { ForbiddenError } from '@vtex/api' import { json } from 'co-body' +import { getUser, setActiveUserByOrganization } from '../Mutations/Users' import { getRole } from '../Queries/Roles' import { getSessionWatcher } from '../Queries/Settings' import { getActiveUserByEmail, getUserByEmail } from '../Queries/Users' import { generateClUser, QUERIES } from './utils' -import { getUser, setActiveUserByOrganization } from '../Mutations/Users' export const Routes = { PROFILE_DOCUMENT_TYPE: 'cpf',