reverted promot prt as admin

This commit is contained in:
Abhijit 2026-11-17 12:47:22 +05:30
parent 22a541d843
commit ec761c811c
3 changed files with 82 additions and 82 deletions

View File

@ -28,7 +28,7 @@ const cohortSchema = new mongoose.Schema(
whatsappError: { type: String, default: '' },
whatsappMeetNotified: { type: Boolean, default: false },
whatsappClosedNotified: { type: Boolean, default: false },
whatsappAdminUser: { type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null }, // therapist promoted as this group's WhatsApp admin
// whatsappAdminUser: { type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null }, // therapist promoted as this group's WhatsApp admin
whatsappMembers: {
type: [{
patient: { type: mongoose.Schema.Types.ObjectId, ref: 'OnboardedPatient', required: true },

View File

@ -151,11 +151,11 @@ function inviteSteps(cohortId, people) {
return people.filter((person) => person.phone).map((person) => inviteStep(cohortId, person));
}
const PRT_ADMIN_MISSING_PHONE = 'Assigned therapist needs a mobile number before they can be a WhatsApp group admin';
// const PRT_ADMIN_MISSING_PHONE = 'Assigned therapist needs a mobile number before they can be a WhatsApp group admin';
// True for old patient add/remove tasks. Therapist admin steps and leave steps still run.
// True for old patient add/remove tasks. Leave steps still run.
export function isRetiredMembershipTask(task) {
if (task?.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) return false;
// if (task?.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) return false;
if (task?.flag === 'leave' && task.kind === 'remove_member') return false;
return ['ensure_contact', 'add_member', 'promote_member', 'remove_member'].includes(task?.kind);
}
@ -186,43 +186,43 @@ async function phoneKeptInGroup(cohort, phone) {
}
// Add then promote the conducting therapist when this cohort does not already record them as admin.
async function prtAdminSteps(cohort) {
const prtId = cohort.virtualPrt?._id || cohort.virtualPrt;
if (!prtId || String(cohort.whatsappAdminUser || '') === String(prtId)) return [];
const user = cohort.virtualPrt?.phone != null
? cohort.virtualPrt
: await User.findById(prtId).select('firstName lastName phone');
const phone = toWhatsappDigits(user?.phone);
if (!phone) {
if (cohort.whatsappError !== PRT_ADMIN_MISSING_PHONE) {
cohort.whatsappError = PRT_ADMIN_MISSING_PHONE;
await cohort.save();
}
return [];
}
if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) {
cohort.whatsappError = '';
await cohort.save();
}
return [
{
kind: 'add_member',
phone,
name: personName(user),
user: prtId,
flag: 'prt',
dedupeKey: `${cohort._id}:prt_add:${prtId}:${phone}`,
},
{
kind: 'promote_member',
phone,
name: personName(user),
user: prtId,
flag: 'prt',
dedupeKey: `${cohort._id}:prt_promote:${prtId}:${phone}`,
},
];
}
// async function prtAdminSteps(cohort) {
// const prtId = cohort.virtualPrt?._id || cohort.virtualPrt;
// if (!prtId || String(cohort.whatsappAdminUser || '') === String(prtId)) return [];
// const user = cohort.virtualPrt?.phone != null
// ? cohort.virtualPrt
// : await User.findById(prtId).select('firstName lastName phone');
// const phone = toWhatsappDigits(user?.phone);
// if (!phone) {
// if (cohort.whatsappError !== PRT_ADMIN_MISSING_PHONE) {
// cohort.whatsappError = PRT_ADMIN_MISSING_PHONE;
// await cohort.save();
// }
// return [];
// }
// if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) {
// cohort.whatsappError = '';
// await cohort.save();
// }
// return [
// {
// kind: 'add_member',
// phone,
// name: personName(user),
// user: prtId,
// flag: 'prt',
// dedupeKey: `${cohort._id}:prt_add:${prtId}:${phone}`,
// },
// {
// kind: 'promote_member',
// phone,
// name: personName(user),
// user: prtId,
// flag: 'prt',
// dedupeKey: `${cohort._id}:prt_promote:${prtId}:${phone}`,
// },
// ];
// }
export async function enqueueCohortGroup(cohortId) {
const cohort = await Cohort.findById(cohortId).populate('virtualPrt', 'firstName lastName phone');
@ -250,7 +250,7 @@ export async function enqueueCohortGroup(cohortId) {
if (!step.phone) continue;
steps.push(inviteStep(cohort._id, step));
}
steps.push(...await prtAdminSteps(cohort));
// steps.push(...await prtAdminSteps(cohort));
await enqueueMany(cohort._id, steps);
return cohort;
}
@ -385,11 +385,11 @@ async function clearCohortError(cohortId) {
await Cohort.updateOne({ _id: cohortId }, { $set: { whatsappError: '' } });
}
function participantAlreadyUpdated(action, err) {
const message = String(err?.message || '').toLowerCase();
if (!message.includes('already') && !message.includes('409')) return false;
return action === 'add' || action === 'promote';
}
// function participantAlreadyUpdated(action, err) {
// const message = String(err?.message || '').toLowerCase();
// if (!message.includes('already') && !message.includes('409')) return false;
// return action === 'add' || action === 'promote';
// }
function participantAlreadyGone(err) {
const message = String(err?.message || '').toLowerCase();
@ -425,34 +425,34 @@ async function runLeaveStep(task) {
}
// Adds the conducting therapist to the group, or promotes them once they are a member.
async function runPrtAdminStep(task) {
const cohort = await Cohort.findById(task.cohort);
if (!cohort?.whatsappGroupJid) {
const waiting = await WhatsAppTask.exists({
cohort: task.cohort,
kind: 'create_group',
status: { $in: OPEN },
});
if (waiting) {
const error = new Error('Waiting for the WhatsApp group');
error.defer = true;
throw error;
}
throw new Error('This cohort has no WhatsApp group');
}
if (!task.phone) throw new Error(PRT_ADMIN_MISSING_PHONE);
const action = task.kind === 'promote_member' ? 'promote' : 'add';
try {
await updateParticipants(cohort.whatsappGroupJid, action, [task.phone]);
} catch (err) {
if (!participantAlreadyUpdated(action, err)) throw err;
}
if (action === 'promote' && task.user) {
cohort.whatsappAdminUser = task.user;
if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) cohort.whatsappError = '';
await cohort.save();
}
}
// async function runPrtAdminStep(task) {
// const cohort = await Cohort.findById(task.cohort);
// if (!cohort?.whatsappGroupJid) {
// const waiting = await WhatsAppTask.exists({
// cohort: task.cohort,
// kind: 'create_group',
// status: { $in: OPEN },
// });
// if (waiting) {
// const error = new Error('Waiting for the WhatsApp group');
// error.defer = true;
// throw error;
// }
// throw new Error('This cohort has no WhatsApp group');
// }
// if (!task.phone) throw new Error(PRT_ADMIN_MISSING_PHONE);
// const action = task.kind === 'promote_member' ? 'promote' : 'add';
// try {
// await updateParticipants(cohort.whatsappGroupJid, action, [task.phone]);
// } catch (err) {
// if (!participantAlreadyUpdated(action, err)) throw err;
// }
// if (action === 'promote' && task.user) {
// cohort.whatsappAdminUser = task.user;
// if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) cohort.whatsappError = '';
// await cohort.save();
// }
// }
async function execute(task) {
if (isRetiredMembershipTask(task)) {
@ -462,10 +462,10 @@ async function execute(task) {
await task.save();
return;
}
if (task.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) {
await runPrtAdminStep(task);
return;
}
// if (task.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) {
// await runPrtAdminStep(task);
// return;
// }
if (task.flag === 'leave' && task.kind === 'remove_member') {
await runLeaveStep(task);
return;
@ -675,7 +675,7 @@ function cancelMembershipTasks() {
$or: [
{ kind: 'ensure_contact' },
{ kind: 'remove_member', flag: { $ne: 'leave' } },
{ kind: { $in: ['add_member', 'promote_member'] }, flag: { $ne: 'prt' } },
{ kind: { $in: ['add_member', 'promote_member'] } },
],
},
{ $set: { status: 'cancelled', lockedAt: null, lastError: 'Membership updates are no longer sent to WhatsApp' } },

View File

@ -11,8 +11,8 @@ test('draws each WhatsApp gap from 15 to 40 seconds', () => {
});
test('keeps the conducting therapist admin steps and drops old membership tasks', () => {
assert.equal(isRetiredMembershipTask({ kind: 'promote_member', flag: 'prt' }), false);
assert.equal(isRetiredMembershipTask({ kind: 'add_member', flag: 'prt' }), false);
// assert.equal(isRetiredMembershipTask({ kind: 'promote_member', flag: 'prt' }), false);
// assert.equal(isRetiredMembershipTask({ kind: 'add_member', flag: 'prt' }), false);
assert.equal(isRetiredMembershipTask({ kind: 'promote_member', flag: '' }), true);
assert.equal(isRetiredMembershipTask({ kind: 'remove_member', flag: 'leave' }), false);
assert.equal(isRetiredMembershipTask({ kind: 'remove_member' }), true);