-
Notifications
You must be signed in to change notification settings - Fork 0
Added lifecycle hooks to solver table #85
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
base: main
Are you sure you want to change the base?
Changes from all commits
5892d45
a7e2fb4
7ea0948
b15c260
f36bff8
c41513f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { errors } from "@strapi/utils"; | ||
|
|
||
| export default { | ||
| async afterCreate(event) { | ||
| try { | ||
| await updateActiveNetworks(event); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be done after updating or creating? |
||
| } catch (error) { | ||
| console.error("Error in beforeCreate:", error); | ||
| throw new errors.ValidationError(error.message || "Error processing solver data"); | ||
| } | ||
| }, | ||
|
|
||
| async afterUpdate(event) { | ||
| try { | ||
| await updateActiveNetworks(event); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these updates and creates will be done through Dagster, what's the advantage of processing this through the lifecycle vs adding this update as a step in the same Dagster job that will do the update that triggers this hook? |
||
| } catch (error) { | ||
| console.error("Error in beforeUpdate:", error); | ||
| throw new errors.ValidationError(error.message || "Error processing solver data"); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| interface SolverData { | ||
| activeNetworks?: string[]; | ||
| hasActiveNetworks?: boolean; | ||
| isServiceFeeEnabled?: boolean; | ||
| solver_networks?: any; | ||
| } | ||
|
|
||
| interface StrapiEvent { | ||
| params: { | ||
| data: SolverData; | ||
| where?: { | ||
| id: number; | ||
| }; | ||
| }; | ||
| result?: { | ||
| id: number; | ||
| }; | ||
| } | ||
|
|
||
| async function updateActiveNetworks(event: StrapiEvent) { | ||
| const { data, where } = event.params; | ||
| const solverData: SolverData = data; | ||
|
|
||
| if (where && !data.solver_networks) { | ||
| try { | ||
| const solver = await strapi.entityService.findOne( | ||
| 'api::solver.solver', | ||
| where.id, | ||
| { populate: ['solver_networks.network'] } | ||
| ); | ||
|
|
||
| if (solver) { | ||
| await updateActiveNetworksData(solver as Solver, solverData); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error fetching solver data for id ${where.id}:`, error); | ||
| throw new errors.ApplicationError(`Failed to fetch solver data: ${error.message}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| interface Solver { | ||
| id: number; | ||
| solver_networks?: Array<{ | ||
| active?: boolean; | ||
| network?: { | ||
| name?: string; | ||
| }; | ||
| }>; | ||
| solver_bonding_pools?: Array<{ | ||
| name?: string; | ||
| joinedOn?: string; | ||
| }>; | ||
| isColocated?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts active network names from solver networks | ||
| * @param solverNetworks Array of solver network objects | ||
| * @returns Array of active network names | ||
| */ | ||
| function getActiveNetworks(solverNetworks?: Array<{ | ||
| active?: boolean; | ||
| network?: { | ||
| name?: string; | ||
| }; | ||
| }>): string[] { | ||
| if (!solverNetworks) { | ||
| return []; | ||
| } | ||
|
|
||
| return solverNetworks | ||
| .filter(network => network.active) | ||
| .map(network => network.network?.name) | ||
| .filter(Boolean) as string[]; // Remove any undefined values | ||
| } | ||
|
|
||
| /** | ||
| * Updates the solver data with active network information | ||
| * This function mutates the data object by setting activeNetworks and hasActiveNetworks properties | ||
| */ | ||
| async function updateActiveNetworksData(solver: Solver, data: SolverData): Promise<void> { | ||
| const activeNetworks = getActiveNetworks(solver.solver_networks); | ||
| data.hasActiveNetworks = activeNetworks.length > 0; | ||
| data.activeNetworks = activeNetworks; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not enough, you should do |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those lifecycle hooks are applied to
solvercollection; shouldn't they be applied tosolver-networkcollection?For example, you have a
solveritem andsolver-networkwhich are coupled. Once you delete thesolver-networkitem, it should update thesolveritem fields.