fix(professors): handle nationalId and phone aliases with trimming; update payment sms
This commit is contained in:
@@ -7,11 +7,10 @@ const eventEmitter = require('../../events/eventEmitter');
|
|||||||
const EVENT_NAMES = require('../../constants/eventNames');
|
const EVENT_NAMES = require('../../constants/eventNames');
|
||||||
const { calculateMeta, escapeRegex, getSearchTerm } = require('../../utils/pagination');
|
const { calculateMeta, escapeRegex, getSearchTerm } = require('../../utils/pagination');
|
||||||
const User = require('../users/userModel');
|
const User = require('../users/userModel');
|
||||||
const { sendInvoiceCreatedSms } = require('../../utils/senders/smsMessages');
|
|
||||||
const logger = require('../../utils/logger');
|
|
||||||
|
|
||||||
const Course = require('../courses/courseModel');
|
const Course = require('../courses/courseModel');
|
||||||
const Class = require('../classes/classModel');
|
const Class = require('../classes/classModel');
|
||||||
|
const { sendInvoiceCreatedSms } = require('../../utils/senders/smsMessages');
|
||||||
|
const logger = require('../../utils/logger');
|
||||||
|
|
||||||
const getAllPayments = async (query) => {
|
const getAllPayments = async (query) => {
|
||||||
const page = parseInt(query.page) || 1;
|
const page = parseInt(query.page) || 1;
|
||||||
@@ -81,17 +80,21 @@ const createPayment = async (body, actorId = null) => {
|
|||||||
if (user?.phoneNumber) {
|
if (user?.phoneNumber) {
|
||||||
let courseName = '';
|
let courseName = '';
|
||||||
if (payment.course) {
|
if (payment.course) {
|
||||||
const courseDoc = await Course.findById(payment.course).select('title').lean();
|
const course = await Course.findById(payment.course).select('title').lean();
|
||||||
courseName = courseDoc?.title || '';
|
if (course?.title) courseName = course.title;
|
||||||
} else if (payment.classes && payment.classes.length > 0) {
|
} else if (payment.classes && payment.classes.length > 0) {
|
||||||
const classDoc = await Class.findById(payment.classes[0]).select('name').lean();
|
const cls = await Class.findById(payment.classes[0]).populate('course', 'title').lean();
|
||||||
courseName = classDoc?.name || '';
|
if (cls?.course?.title) {
|
||||||
|
courseName = cls.course.title;
|
||||||
|
} else if (cls?.name) {
|
||||||
|
courseName = cls.name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendInvoiceCreatedSms(user.phoneNumber, {
|
await sendInvoiceCreatedSms(user.phoneNumber, {
|
||||||
fullName: user.name || '',
|
fullName: user.name || '',
|
||||||
amount: payment.amount,
|
amount: payment.amount,
|
||||||
course: courseName
|
course: courseName || '-'
|
||||||
}, user._id);
|
}, user._id);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -7,18 +7,27 @@ const EVENT_NAMES = require('../../constants/eventNames');
|
|||||||
const { parsePaginationAndSort, buildFilterQuery, calculateMeta } = require('../../utils/pagination');
|
const { parsePaginationAndSort, buildFilterQuery, calculateMeta } = require('../../utils/pagination');
|
||||||
|
|
||||||
const createProfessor = async (data) => {
|
const createProfessor = async (data) => {
|
||||||
|
const payload = {
|
||||||
|
...data,
|
||||||
|
name: String(data.name || '').trim(),
|
||||||
|
surname: String(data.surname || '').trim(),
|
||||||
|
nationalIdCode: String(data.nationalIdCode || data.nationalId || '').trim(),
|
||||||
|
phoneNumber: String(data.phoneNumber || data.phone || '').trim(),
|
||||||
|
email: data.email ? String(data.email).trim().toLowerCase() : undefined
|
||||||
|
};
|
||||||
|
|
||||||
const existing = await Professor.findOne({
|
const existing = await Professor.findOne({
|
||||||
$or: [
|
$or: [
|
||||||
{ nationalIdCode: data.nationalIdCode },
|
{ nationalIdCode: payload.nationalIdCode },
|
||||||
{ phoneNumber: data.phoneNumber },
|
{ phoneNumber: payload.phoneNumber },
|
||||||
...(data.email ? [{ email: data.email }] : [])
|
...(payload.email ? [{ email: payload.email }] : [])
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
if (existing) {
|
if (existing) {
|
||||||
throw new AppError('PROFESSOR_ALREADY_EXISTS');
|
throw new AppError('PROFESSOR_ALREADY_EXISTS');
|
||||||
}
|
}
|
||||||
|
|
||||||
const professor = await Professor.create(data);
|
const professor = await Professor.create(payload);
|
||||||
eventEmitter.emit(EVENT_NAMES.PROFESSOR_CREATED, { professorId: professor._id, name: `${professor.name} ${professor.surname}` });
|
eventEmitter.emit(EVENT_NAMES.PROFESSOR_CREATED, { professorId: professor._id, name: `${professor.name} ${professor.surname}` });
|
||||||
return professor;
|
return professor;
|
||||||
};
|
};
|
||||||
@@ -50,7 +59,22 @@ const updateProfessor = async (id, updateData) => {
|
|||||||
throw new AppError('PROFESSOR_NOT_FOUND');
|
throw new AppError('PROFESSOR_NOT_FOUND');
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(professor, updateData);
|
const payload = { ...updateData };
|
||||||
|
if (payload.nationalIdCode !== undefined || payload.nationalId !== undefined) {
|
||||||
|
payload.nationalIdCode = String(payload.nationalIdCode || payload.nationalId || '').trim();
|
||||||
|
delete payload.nationalId;
|
||||||
|
}
|
||||||
|
if (payload.phoneNumber !== undefined || payload.phone !== undefined) {
|
||||||
|
payload.phoneNumber = String(payload.phoneNumber || payload.phone || '').trim();
|
||||||
|
delete payload.phone;
|
||||||
|
}
|
||||||
|
if (payload.name !== undefined) payload.name = String(payload.name).trim();
|
||||||
|
if (payload.surname !== undefined) payload.surname = String(payload.surname).trim();
|
||||||
|
if (payload.email !== undefined) {
|
||||||
|
payload.email = payload.email ? String(payload.email).trim().toLowerCase() : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(professor, payload);
|
||||||
await professor.save();
|
await professor.save();
|
||||||
return professor;
|
return professor;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -61,40 +61,31 @@ const sendClassReminderSms = async (receiver, className, time, place = '', userI
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendInvoiceCreatedSms = async (receiver, payloadOrAmount, userId = null, extraCourse = '') => {
|
const sendInvoiceCreatedSms = async (receiver, payload, userId = null) => {
|
||||||
const isObject = typeof payloadOrAmount === 'object' && payloadOrAmount !== null;
|
const resolvedUserId = userId || await resolveUserIdByPhone(receiver);
|
||||||
const resolvedUserId = (isObject && payloadOrAmount.userId)
|
const { templateId, variables } = await getSmsTemplate('invoiceCreated');
|
||||||
? payloadOrAmount.userId
|
|
||||||
: (userId || await resolveUserIdByPhone(receiver));
|
|
||||||
|
|
||||||
let fullName = '';
|
let fullName = '';
|
||||||
let amount = 0;
|
let amount = '';
|
||||||
let course = '';
|
let course = '';
|
||||||
|
|
||||||
if (isObject) {
|
if (typeof payload === 'object' && payload !== null) {
|
||||||
fullName = payloadOrAmount.fullName || '';
|
fullName = payload.fullName || '';
|
||||||
amount = payloadOrAmount.amount ?? 0;
|
amount = payload.amount != null ? String(payload.amount) : '';
|
||||||
course = payloadOrAmount.course || '';
|
course = payload.course || '';
|
||||||
} else {
|
} else {
|
||||||
amount = payloadOrAmount;
|
amount = String(payload || '');
|
||||||
course = typeof extraCourse === 'string' ? extraCourse : '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fullName && resolvedUserId) {
|
|
||||||
const user = await User.findById(resolvedUserId).select('name').lean();
|
|
||||||
fullName = user?.name || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
const { templateId, variables } = await getSmsTemplate('invoiceCreated');
|
|
||||||
const amountLabel = String(amount);
|
const amountLabel = String(amount);
|
||||||
const fullNameLabel = String(fullName || '').trim();
|
const courseLabel = course || '-';
|
||||||
const courseLabel = String(course || '').trim();
|
const fullNameLabel = fullName || 'کارآموز';
|
||||||
|
|
||||||
return recordAndSend({
|
return recordAndSend({
|
||||||
userId: resolvedUserId,
|
userId: resolvedUserId,
|
||||||
channel: 'sms',
|
channel: 'sms',
|
||||||
subject: 'ایجاد صورتحساب',
|
subject: 'ایجاد صورتحساب',
|
||||||
body: `صورتحساب جدید به مبلغ ${amountLabel} تومان بابت دوره «${courseLabel || '-'}» برای ${fullNameLabel || 'کارآموز'} صادر شد.`,
|
body: `کارآموز عزیز، ${fullNameLabel}، یک صورتحساب به مبلغ ${amountLabel} تومان بابت دوره «${courseLabel}» برای شما ایجاد شد.`,
|
||||||
relatedEvent: 'payment.created',
|
relatedEvent: 'payment.created',
|
||||||
sendFn: () => sendSingleSms(receiver, templateId, buildSmsParameters(variables, {
|
sendFn: () => sendSingleSms(receiver, templateId, buildSmsParameters(variables, {
|
||||||
fullName: fullNameLabel,
|
fullName: fullNameLabel,
|
||||||
|
|||||||
Reference in New Issue
Block a user