Skip to content

Commit a9888bf

Browse files
committed
Update service fee flag based on bonding pool status
1 parent 8c3b827 commit a9888bf

File tree

1 file changed

+106
-0
lines changed
  • src/api/solver-bonding-pool/content-types/solver-bonding-pool

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { updateServiceFeeEnabledForSolver } from '../../../solver/content-types/solver/lifecycles';
2+
3+
export default {
4+
async afterCreate(event) {
5+
await updateRelatedSolvers(event);
6+
},
7+
8+
async afterUpdate(event) {
9+
await updateRelatedSolvers(event);
10+
},
11+
12+
async beforeDelete(event) {
13+
await storeRelatedSolversForUpdate(event);
14+
},
15+
16+
async afterDelete(event) {
17+
await updateStoredSolvers(event);
18+
},
19+
};
20+
21+
// Store for temporarily keeping solver IDs between beforeDelete and afterDelete
22+
const solverIdsToUpdate = new Map();
23+
24+
async function updateRelatedSolvers(event) {
25+
try {
26+
const { result } = event;
27+
28+
// If this is a delete operation, result might not be available
29+
if (!result || !result.solvers) {
30+
return;
31+
}
32+
33+
// Get the solver IDs
34+
const solverIds = Array.isArray(result.solvers)
35+
? result.solvers.map(solver => solver.id || solver)
36+
: [];
37+
38+
// If no solvers are directly available in the result, fetch them
39+
if (solverIds.length === 0 && result.id) {
40+
const bondingPool = await strapi.entityService.findOne(
41+
'api::solver-bonding-pool.solver-bonding-pool',
42+
result.id,
43+
{ populate: ['solvers'] }
44+
);
45+
46+
if (bondingPool && bondingPool.solvers) {
47+
bondingPool.solvers.forEach(solver => {
48+
solverIds.push(solver.id);
49+
});
50+
}
51+
}
52+
53+
// Update each solver's service fee enabled status
54+
for (const solverId of solverIds) {
55+
if (solverId) {
56+
await updateServiceFeeEnabledForSolver(solverId);
57+
}
58+
}
59+
} catch (error) {
60+
console.error('Error updating related solvers:', error);
61+
}
62+
}
63+
64+
async function storeRelatedSolversForUpdate(event) {
65+
try {
66+
const { where } = event.params;
67+
const { id } = where;
68+
69+
// Get the bonding pool with its solvers
70+
const bondingPool = await strapi.entityService.findOne(
71+
'api::solver-bonding-pool.solver-bonding-pool',
72+
id,
73+
{ populate: ['solvers'] }
74+
);
75+
76+
if (bondingPool && bondingPool.solvers) {
77+
// Store the solver IDs for later use in afterDelete
78+
const solverIds = bondingPool.solvers.map(solver => solver.id);
79+
solverIdsToUpdate.set(id, solverIds);
80+
}
81+
} catch (error) {
82+
console.error('Error storing related solvers for update:', error);
83+
}
84+
}
85+
86+
async function updateStoredSolvers(event) {
87+
try {
88+
const { where } = event.params;
89+
const { id } = where;
90+
91+
// Get the stored solver IDs
92+
const solverIds = solverIdsToUpdate.get(id) || [];
93+
94+
// Update each solver's service fee enabled status
95+
for (const solverId of solverIds) {
96+
if (solverId) {
97+
await updateServiceFeeEnabledForSolver(solverId);
98+
}
99+
}
100+
101+
// Clean up the stored solver IDs
102+
solverIdsToUpdate.delete(id);
103+
} catch (error) {
104+
console.error('Error updating stored solvers:', error);
105+
}
106+
}

0 commit comments

Comments
 (0)