-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): allow to create update record custom actions from the fr…
…ontend (#729)
- Loading branch information
Showing
24 changed files
with
706 additions
and
533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
packages/agent/src/services/model-customizations/action-customization.ts
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
packages/agent/src/services/model-customizations/actions/get-actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
ActionConfiguration, | ||
ActionType, | ||
ModelCustomization, | ||
ModelCustomizationType, | ||
} from '@forestadmin/forestadmin-client'; | ||
|
||
export default function getActions<TConfiguration extends ActionConfiguration>( | ||
type: ActionType, | ||
configuration: ModelCustomization[], | ||
): ModelCustomization<TConfiguration>[] { | ||
return configuration.filter( | ||
customization => | ||
customization.type === ModelCustomizationType.action && | ||
(customization as ModelCustomization<TConfiguration>).configuration.type === type, | ||
) as ModelCustomization<TConfiguration>[]; | ||
} |
40 changes: 40 additions & 0 deletions
40
...ges/agent/src/services/model-customizations/actions/update-record/update-record-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Plugin } from '@forestadmin/datasource-customizer'; | ||
import { | ||
ModelCustomization, | ||
UpdateRecordActionConfiguration, | ||
} from '@forestadmin/forestadmin-client'; | ||
|
||
import getActions from '../get-actions'; | ||
|
||
export default class UpdateRecordActionsPlugin { | ||
public static VERSION = '1.0.0'; | ||
public static FEATURE = 'update-record-actions'; | ||
|
||
public static addUpdateRecordActions: Plugin<ModelCustomization[]> = ( | ||
datasourceCustomizer, | ||
_, | ||
modelCustomizations, | ||
) => { | ||
const actions = getActions<UpdateRecordActionConfiguration>( | ||
'update-record', | ||
modelCustomizations, | ||
); | ||
|
||
actions.forEach(action => { | ||
const collection = datasourceCustomizer.getCollection(action.modelName); | ||
|
||
collection.addAction(action.name, { | ||
scope: action.configuration.scope, | ||
execute: async context => { | ||
const { | ||
configuration: { | ||
configuration: { fields }, | ||
}, | ||
} = action; | ||
|
||
await context.collection.update(context.filter, fields); | ||
}, | ||
}); | ||
}); | ||
}; | ||
} |
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
packages/agent/src/services/model-customizations/actions/webhook/webhook-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Plugin } from '@forestadmin/datasource-customizer'; | ||
import { ModelCustomization, WebhookActionConfiguration } from '@forestadmin/forestadmin-client'; | ||
|
||
import executeWebhook from './execute-webhook'; | ||
import getActions from '../get-actions'; | ||
|
||
export default class WebhookActionsPlugin { | ||
public static VERSION = '1.0.0'; | ||
public static FEATURE = 'webhook-custom-actions'; | ||
|
||
public static addWebhookActions: Plugin<ModelCustomization[]> = ( | ||
datasourceCustomizer, | ||
_, | ||
modelCustomizations, | ||
) => { | ||
const actions = getActions<WebhookActionConfiguration>('webhook', modelCustomizations); | ||
actions.forEach(action => { | ||
const collection = datasourceCustomizer.getCollection(action.modelName); | ||
|
||
collection.addAction(action.name, { | ||
scope: action.configuration.scope, | ||
execute: context => executeWebhook(action, context), | ||
}); | ||
}); | ||
}; | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/agent/src/services/model-customizations/customization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Plugin } from '@forestadmin/datasource-customizer'; | ||
import { ForestAdminClient, ModelCustomization } from '@forestadmin/forestadmin-client'; | ||
|
||
import UpdateRecordActionsPlugin from './actions/update-record/update-record-plugin'; | ||
import WebhookActionsPlugin from './actions/webhook/webhook-plugin'; | ||
import { AgentOptionsWithDefaults } from '../../types'; | ||
|
||
type ExperimentalOptions = AgentOptionsWithDefaults['experimental']; | ||
|
||
const optionsToFeatureMapping: Record<keyof ExperimentalOptions, string> = { | ||
webhookCustomActions: WebhookActionsPlugin.FEATURE, | ||
updateRecordCustomActions: UpdateRecordActionsPlugin.FEATURE, | ||
}; | ||
|
||
const featuresFormattedWithVersion = [ | ||
{ feature: WebhookActionsPlugin.FEATURE, version: WebhookActionsPlugin.VERSION }, | ||
{ feature: UpdateRecordActionsPlugin.FEATURE, version: UpdateRecordActionsPlugin.VERSION }, | ||
]; | ||
|
||
export default class CustomizationPluginService { | ||
private readonly client: ForestAdminClient; | ||
|
||
private readonly options: AgentOptionsWithDefaults; | ||
|
||
public constructor(agentOptions: AgentOptionsWithDefaults) { | ||
this.client = agentOptions.forestAdminClient; | ||
|
||
this.options = agentOptions; | ||
} | ||
|
||
public addCustomizations: Plugin<void> = async (datasourceCustomizer, _) => { | ||
const { experimental } = this.options; | ||
if (!experimental) return; | ||
|
||
const modelCustomizations = await this.client.modelCustomizationService.getConfiguration(); | ||
|
||
CustomizationPluginService.makeAddCustomizations(experimental)( | ||
datasourceCustomizer, | ||
_, | ||
modelCustomizations, | ||
); | ||
}; | ||
|
||
public static makeAddCustomizations: ( | ||
experimental: ExperimentalOptions, | ||
) => Plugin<ModelCustomization[]> = (experimental: ExperimentalOptions) => { | ||
return (datasourceCustomizer, _, modelCustomizations) => { | ||
if (experimental.webhookCustomActions) { | ||
WebhookActionsPlugin.addWebhookActions(datasourceCustomizer, _, modelCustomizations); | ||
} | ||
|
||
if (experimental.updateRecordCustomActions) { | ||
UpdateRecordActionsPlugin.addUpdateRecordActions( | ||
datasourceCustomizer, | ||
_, | ||
modelCustomizations, | ||
); | ||
} | ||
}; | ||
}; | ||
|
||
public buildFeatures() { | ||
return CustomizationPluginService.buildFeatures(this.options?.experimental); | ||
} | ||
|
||
public static buildFeatures(experimental: ExperimentalOptions): Record<string, string> | null { | ||
const features = CustomizationPluginService.getFeatures(experimental); | ||
|
||
const enabledFeaturesFormattedWithVersion = featuresFormattedWithVersion | ||
.filter(({ feature }) => features?.includes(feature)) | ||
.reduce( | ||
(acc, { feature, version }) => ({ | ||
...acc, | ||
[feature]: version, | ||
}), | ||
{}, | ||
); | ||
|
||
return Object.keys(enabledFeaturesFormattedWithVersion).length | ||
? enabledFeaturesFormattedWithVersion | ||
: null; | ||
} | ||
|
||
private static getFeatures(experimental: ExperimentalOptions): string[] { | ||
return Object.entries(optionsToFeatureMapping) | ||
.filter(([experimentalFeature]) => experimental?.[experimentalFeature]) | ||
.map(([, feature]) => feature); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
packages/agent/test/__factories__/model-customizations/action-customization.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.