50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
export async function applyOps(opsObj, { req, userId, scenarioId }) {
|
|
if (!Array.isArray(opsObj?.milestones)) return [];
|
|
|
|
const apiBase = process.env.APTIVA_INTERNAL_API || 'http://localhost:5002/api';
|
|
const auth = (p, o = {}) => internalFetch(req, `${apiBase}${p}`, o);
|
|
|
|
const confirmations = [];
|
|
|
|
for (const m of opsObj.milestones) {
|
|
const op = (m?.op || '').toUpperCase();
|
|
|
|
/* ---------- DELETE ---------- */
|
|
if (op === 'DELETE' && m.id) {
|
|
const cleanId = m.id.trim();
|
|
const r = await auth(`/premium/milestones/${cleanId}`, { method: 'DELETE' });
|
|
if (r.ok) confirmations.push(`Deleted milestone ${cleanId}`);
|
|
continue;
|
|
}
|
|
|
|
/* ---------- UPDATE ---------- */
|
|
if (op === 'UPDATE' && m.id && m.patch) {
|
|
const r = await auth(`/premium/milestones/${m.id}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(m.patch),
|
|
});
|
|
if (r.ok) confirmations.push(`Updated milestone ${m.id}`);
|
|
continue;
|
|
}
|
|
|
|
/* ---------- CREATE ---------- */
|
|
if (op === 'CREATE' && m.data) {
|
|
// inject career_profile_id if the bot forgot it
|
|
m.data.career_profile_id = m.data.career_profile_id || scenarioId;
|
|
const r = await auth('/premium/milestone', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(m.data),
|
|
});
|
|
if (r.ok) {
|
|
const j = await r.json();
|
|
const newId = Array.isArray(j) ? j[0]?.id : j.id;
|
|
confirmations.push(`Created milestone ${newId || '(new)'}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return confirmations;
|
|
}
|