Skip to content

Commit

Permalink
fix(agent): make sure that triggered actions from the frontend execut…
Browse files Browse the repository at this point in the history
…e the right code even after an addition or removal of custom actions (#706)
  • Loading branch information
Thenkei authored May 30, 2023
1 parent 949b943 commit 9581036
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 6 additions & 4 deletions packages/agent/src/routes/modification/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ export default class ActionRoute extends CollectionRoute {
}

setupRoutes(router: Router): void {
// Generate url that matches the declaration in forest-schema/generator-actions.ts
const actionIndex = Object.keys(this.collection.schema.actions).indexOf(this.actionName);
const path = `/_actions/${this.collection.name}/${actionIndex}`;
const slug = SchemaGeneratorActions.getActionSlug(this.actionName);
const path = `/_actions/${this.collection.name}/${actionIndex}/${slug}`;

router.post(
`${path}/:slug`,
`${path}`,
this.middlewareCustomActionApprovalRequestData.bind(this),
this.handleExecute.bind(this),
);
router.post(`${path}/:slug/hooks/load`, this.handleHook.bind(this));
router.post(`${path}/:slug/hooks/change`, this.handleHook.bind(this));
router.post(`${path}/hooks/load`, this.handleHook.bind(this));
router.post(`${path}/hooks/change`, this.handleHook.bind(this));
}

private async handleExecute(context: Context): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion packages/agent/src/utils/forest-schema/generator-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ export default class SchemaGeneratorActions {
},
];

static getActionSlug(name: string) {
return name.toLocaleLowerCase().replace(/[^a-z0-9-]+/g, '-');
}

static async buildSchema(collection: Collection, name: string): Promise<ForestServerAction> {
const schema = collection.schema.actions[name];
const actionIndex = Object.keys(collection.schema.actions).indexOf(name);

// Generate url-safe friendly name (which won't be unique, but that's OK).
const slug = name.toLocaleLowerCase().replace(/[^a-z0-9-]+/g, '-');
const slug = SchemaGeneratorActions.getActionSlug(name);
const fields = await SchemaGeneratorActions.buildFields(collection, name, schema);

return {
Expand Down
10 changes: 5 additions & 5 deletions packages/agent/test/routes/modification/action/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('ActionRoute', () => {
dataSource = factories.dataSource.buildWithCollections([
factories.collection.build({
name: 'books',
schema: { actions: { MySingleAction: null } },
schema: { actions: { MySingleAction: undefined } },
getForm: jest.fn(),
execute: jest.fn(),
}),
Expand All @@ -64,16 +64,16 @@ describe('ActionRoute', () => {
route.setupRoutes(router);

expect(router.post).toHaveBeenCalledWith(
'/_actions/books/0/:slug',
'/_actions/books/0/mysingleaction',
expect.any(Function), // middlewareCustomActionApprovalRequestData
expect.any(Function),
);
expect(router.post).toHaveBeenCalledWith(
'/_actions/books/0/:slug/hooks/load',
'/_actions/books/0/mysingleaction/hooks/load',
expect.any(Function),
);
expect(router.post).toHaveBeenCalledWith(
'/_actions/books/0/:slug/hooks/change',
'/_actions/books/0/mysingleaction/hooks/change',
expect.any(Function),
);
});
Expand All @@ -83,7 +83,7 @@ describe('ActionRoute', () => {
dataSource = factories.dataSource.buildWithCollections([
factories.collection.build({
name: 'books',
schema: { actions: { My_Action: null } },
schema: { actions: { My_Action: undefined } },
getForm: jest.fn(),
execute: jest.fn(),
}),
Expand Down

0 comments on commit 9581036

Please sign in to comment.