Skip to content
Open
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
108 changes: 108 additions & 0 deletions src/api/solver/content-types/solver/lifecycles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { errors } from "@strapi/utils";

export default {
Copy link
Contributor

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 solver collection; shouldn't they be applied to solver-network collection?
For example, you have a solver item and solver-network which are coupled. Once you delete the solver-network item, it should update the solver item fields.

async afterCreate(event) {
try {
await updateActiveNetworks(event);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not enough, you should do entityService.update(...) with the updates

}
Loading