Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(agent): make sure that triggered actions from the frontend execute the right code even after an addition or removal of custom actions #706

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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