Skip to content

Commit

Permalink
feat: setup events on app installation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudge committed Nov 9, 2023
1 parent fb68d4d commit 3fe8e75
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
"host": "rc.vtex.com",
"path": "/api/analytics/schemaless-events"
}
},
{
"name": "colossus-fire-event"
},
{
"name": "colossus-write-logs"
}
],
"settingsSchema": {
Expand Down
24 changes: 24 additions & 0 deletions node/config/setupAppConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { setupAppSettings } from '../resolvers/Queries/Settings'

const setupAppConfiguration = async (
ctx: InstalledAppEvent,
next: () => Promise<any>
) => {
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
20 changes: 15 additions & 5 deletions node/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,10 +64,19 @@ declare global {
interface State {
code: number
}

interface InstalledAppEvent extends EventContext<Clients> {
body: { id?: string }
}
}

export default new Service<Clients, RecorderState, ParamsContext>({
clients,
events: {
onAppInstalled: setupAppConfiguration,
onAppLinked: setupAppConfiguration,
onSettingsChanged: setupAppConfiguration,
},
graphql: {
resolvers: {
Mutation: resolvers.Mutation,
Expand Down
6 changes: 5 additions & 1 deletion node/resolvers/Queries/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion node/resolvers/Routes/index.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit 3fe8e75

Please sign in to comment.