Skip to content

Commit

Permalink
feat(caller): add the current visited url of the user
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBunlon committed Jan 26, 2024
1 parent ce0a389 commit a35be3a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 9 deletions.
5 changes: 1 addition & 4 deletions packages/agent/src/routes/modification/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export default class ActionRoute extends CollectionRoute {
this.getRecordSelection(context, false),
]);
const requestBody = context.request.body as SmartActionApprovalRequestBody;
const webAppURL: URL = new URL(context.request.url || context.request.originalUrl);

const canPerformCustomActionParams = {
caller,
Expand Down Expand Up @@ -104,9 +103,7 @@ export default class ActionRoute extends CollectionRoute {

// Now that we have the field list, we can parse the data again.
const data = ForestValueConverter.makeFormData(dataSource, rawData, fields);
const result = await this.collection.execute(caller, this.actionName, data, filterForCaller, {
webAppURL,
});
const result = await this.collection.execute(caller, this.actionName, data, filterForCaller);

if (result?.type === 'Error') {
context.response.status = HttpCode.BadRequest;
Expand Down
7 changes: 6 additions & 1 deletion packages/agent/src/utils/query-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ export default class QueryStringParser {
QueryStringParser.VALID_TIMEZONES.add(timezone);
}

return { ...context.state.user, timezone, requestId: uuidv4() };
return {
...context.state.user,
timezone,
requestId: uuidv4(),
webAppURL: new URL(context.originalUrl),
};
}

static parsePagination(context: Context): Page {
Expand Down
15 changes: 15 additions & 0 deletions packages/agent/test/utils/query-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ describe('QueryStringParser', () => {
});
});

test('should return the current visited url from user eg forestContextUrl', () => {
const context = createMockContext({
state: { user: { email: '[email protected]' } },
customProperties: { query: { timezone: 'America/Los_Angeles' } },
url: 'https://app.forestadmin.com/aProject/anEnvironment/aRendering',
});

expect(QueryStringParser.parseCaller(context)).toEqual({
email: '[email protected]',
requestId: expect.any(String),
timezone: 'America/Los_Angeles',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
});
});

test('should throw a ValidationError when the timezone is missing', () => {
const context = createMockContext({
customProperties: { query: {} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ActionCollectionDecorator extends CollectionDecorator {
if (!action) return this.childCollection.execute(caller, name, data, filter);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const context = this.getContext(caller, action, data, filter) as any;
const context = this.getContext(caller, action, data, filter, null, null) as any;
const resultBuilder = new ResultBuilder();
const result = await action.execute(context, resultBuilder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class CacheDataSourceInterface {
team: 'system',
timezone: 'UTC',
requestId: '',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
};

constructor(dataSource: DataSource) {
Expand Down
1 change: 1 addition & 0 deletions packages/datasource-toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export * from './interfaces/query/sort';
export * from './interfaces/query/page';
export * from './interfaces/record';
export * from './interfaces/schema';

export {
GenericTree,
GenericTreeBranch,
Expand Down
1 change: 1 addition & 0 deletions packages/datasource-toolkit/src/interfaces/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type Caller = {
role: string;
tags: { [key: string]: string };
timezone: string;
webAppURL: URL;
};
3 changes: 0 additions & 3 deletions packages/datasource-toolkit/src/interfaces/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export interface Collection {
name: string,
formValues: RecordData,
filter?: Filter,
additionalInformation?: {
webAppURL: URL;
},
): Promise<ActionResult>;

getForm(
Expand Down
1 change: 1 addition & 0 deletions packages/datasource-toolkit/test/__factories__/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default Factory.define<Caller>(() => ({
tags: {},
team: 'team',
timezone: 'Europe/Paris',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
}));

0 comments on commit a35be3a

Please sign in to comment.