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: '' }, whatsappError: { type: String, default: '' },
whatsappMeetNotified: { type: Boolean, default: false }, whatsappMeetNotified: { type: Boolean, default: false },
whatsappClosedNotified: { 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: { whatsappMembers: {
type: [{ type: [{
patient: { type: mongoose.Schema.Types.ObjectId, ref: 'OnboardedPatient', required: true }, 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)); 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) { 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; if (task?.flag === 'leave' && task.kind === 'remove_member') return false;
return ['ensure_contact', 'add_member', 'promote_member', 'remove_member'].includes(task?.kind); 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. // Add then promote the conducting therapist when this cohort does not already record them as admin.
async function prtAdminSteps(cohort) { // async function prtAdminSteps(cohort) {
const prtId = cohort.virtualPrt?._id || cohort.virtualPrt; // const prtId = cohort.virtualPrt?._id || cohort.virtualPrt;
if (!prtId || String(cohort.whatsappAdminUser || '') === String(prtId)) return []; // if (!prtId || String(cohort.whatsappAdminUser || '') === String(prtId)) return [];
const user = cohort.virtualPrt?.phone != null // const user = cohort.virtualPrt?.phone != null
? cohort.virtualPrt // ? cohort.virtualPrt
: await User.findById(prtId).select('firstName lastName phone'); // : await User.findById(prtId).select('firstName lastName phone');
const phone = toWhatsappDigits(user?.phone); // const phone = toWhatsappDigits(user?.phone);
if (!phone) { // if (!phone) {
if (cohort.whatsappError !== PRT_ADMIN_MISSING_PHONE) { // if (cohort.whatsappError !== PRT_ADMIN_MISSING_PHONE) {
cohort.whatsappError = PRT_ADMIN_MISSING_PHONE; // cohort.whatsappError = PRT_ADMIN_MISSING_PHONE;
await cohort.save(); // await cohort.save();
} // }
return []; // return [];
} // }
if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) { // if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) {
cohort.whatsappError = ''; // cohort.whatsappError = '';
await cohort.save(); // await cohort.save();
} // }
return [ // return [
{ // {
kind: 'add_member', // kind: 'add_member',
phone, // phone,
name: personName(user), // name: personName(user),
user: prtId, // user: prtId,
flag: 'prt', // flag: 'prt',
dedupeKey: `${cohort._id}:prt_add:${prtId}:${phone}`, // dedupeKey: `${cohort._id}:prt_add:${prtId}:${phone}`,
}, // },
{ // {
kind: 'promote_member', // kind: 'promote_member',
phone, // phone,
name: personName(user), // name: personName(user),
user: prtId, // user: prtId,
flag: 'prt', // flag: 'prt',
dedupeKey: `${cohort._id}:prt_promote:${prtId}:${phone}`, // dedupeKey: `${cohort._id}:prt_promote:${prtId}:${phone}`,
}, // },
]; // ];
} // }
export async function enqueueCohortGroup(cohortId) { export async function enqueueCohortGroup(cohortId) {
const cohort = await Cohort.findById(cohortId).populate('virtualPrt', 'firstName lastName phone'); const cohort = await Cohort.findById(cohortId).populate('virtualPrt', 'firstName lastName phone');
@ -250,7 +250,7 @@ export async function enqueueCohortGroup(cohortId) {
if (!step.phone) continue; if (!step.phone) continue;
steps.push(inviteStep(cohort._id, step)); steps.push(inviteStep(cohort._id, step));
} }
steps.push(...await prtAdminSteps(cohort)); // steps.push(...await prtAdminSteps(cohort));
await enqueueMany(cohort._id, steps); await enqueueMany(cohort._id, steps);
return cohort; return cohort;
} }
@ -385,11 +385,11 @@ async function clearCohortError(cohortId) {
await Cohort.updateOne({ _id: cohortId }, { $set: { whatsappError: '' } }); await Cohort.updateOne({ _id: cohortId }, { $set: { whatsappError: '' } });
} }
function participantAlreadyUpdated(action, err) { // function participantAlreadyUpdated(action, err) {
const message = String(err?.message || '').toLowerCase(); // const message = String(err?.message || '').toLowerCase();
if (!message.includes('already') && !message.includes('409')) return false; // if (!message.includes('already') && !message.includes('409')) return false;
return action === 'add' || action === 'promote'; // return action === 'add' || action === 'promote';
} // }
function participantAlreadyGone(err) { function participantAlreadyGone(err) {
const message = String(err?.message || '').toLowerCase(); 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. // Adds the conducting therapist to the group, or promotes them once they are a member.
async function runPrtAdminStep(task) { // async function runPrtAdminStep(task) {
const cohort = await Cohort.findById(task.cohort); // const cohort = await Cohort.findById(task.cohort);
if (!cohort?.whatsappGroupJid) { // if (!cohort?.whatsappGroupJid) {
const waiting = await WhatsAppTask.exists({ // const waiting = await WhatsAppTask.exists({
cohort: task.cohort, // cohort: task.cohort,
kind: 'create_group', // kind: 'create_group',
status: { $in: OPEN }, // status: { $in: OPEN },
}); // });
if (waiting) { // if (waiting) {
const error = new Error('Waiting for the WhatsApp group'); // const error = new Error('Waiting for the WhatsApp group');
error.defer = true; // error.defer = true;
throw error; // throw error;
} // }
throw new Error('This cohort has no WhatsApp group'); // throw new Error('This cohort has no WhatsApp group');
} // }
if (!task.phone) throw new Error(PRT_ADMIN_MISSING_PHONE); // if (!task.phone) throw new Error(PRT_ADMIN_MISSING_PHONE);
const action = task.kind === 'promote_member' ? 'promote' : 'add'; // const action = task.kind === 'promote_member' ? 'promote' : 'add';
try { // try {
await updateParticipants(cohort.whatsappGroupJid, action, [task.phone]); // await updateParticipants(cohort.whatsappGroupJid, action, [task.phone]);
} catch (err) { // } catch (err) {
if (!participantAlreadyUpdated(action, err)) throw err; // if (!participantAlreadyUpdated(action, err)) throw err;
} // }
if (action === 'promote' && task.user) { // if (action === 'promote' && task.user) {
cohort.whatsappAdminUser = task.user; // cohort.whatsappAdminUser = task.user;
if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) cohort.whatsappError = ''; // if (cohort.whatsappError === PRT_ADMIN_MISSING_PHONE) cohort.whatsappError = '';
await cohort.save(); // await cohort.save();
} // }
} // }
async function execute(task) { async function execute(task) {
if (isRetiredMembershipTask(task)) { if (isRetiredMembershipTask(task)) {
@ -462,10 +462,10 @@ async function execute(task) {
await task.save(); await task.save();
return; return;
} }
if (task.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) { // if (task.flag === 'prt' && (task.kind === 'add_member' || task.kind === 'promote_member')) {
await runPrtAdminStep(task); // await runPrtAdminStep(task);
return; // return;
} // }
if (task.flag === 'leave' && task.kind === 'remove_member') { if (task.flag === 'leave' && task.kind === 'remove_member') {
await runLeaveStep(task); await runLeaveStep(task);
return; return;
@ -675,7 +675,7 @@ function cancelMembershipTasks() {
$or: [ $or: [
{ kind: 'ensure_contact' }, { kind: 'ensure_contact' },
{ kind: 'remove_member', flag: { $ne: 'leave' } }, { 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' } }, { $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', () => { 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: 'promote_member', flag: 'prt' }), false);
assert.equal(isRetiredMembershipTask({ kind: 'add_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: 'promote_member', flag: '' }), true);
assert.equal(isRetiredMembershipTask({ kind: 'remove_member', flag: 'leave' }), false); assert.equal(isRetiredMembershipTask({ kind: 'remove_member', flag: 'leave' }), false);
assert.equal(isRetiredMembershipTask({ kind: 'remove_member' }), true); assert.equal(isRetiredMembershipTask({ kind: 'remove_member' }), true);