Untitled
unknown
plain_text
a month ago
5.2 kB
1
Indexable
Never
const groupIdCustomer = uuidv4(); const groupIdOnshore = uuidv4(); const inquiry = question.map((q, index) => { const currentMilliseconds = new Date(); const newMilliseconds = currentMilliseconds.getMilliseconds() + index; currentMilliseconds.setMilliseconds(newMilliseconds); const updatedAt = new Date(currentMilliseconds); let groupId = groupIdOnshore; if (q.receiver.includes('customer')) { groupId = groupIdCustomer; } if (!isCdCm) { groupId = uuidv4(); } return { content: q.content, field: q.field, inqType: q.inqType, ansType: q.ansType, receiver: q.receiver, mybl: blId, createdBy: userId, updatedBy: userId, updatedAt, groupId, }; }); const answer = []; for (const q of question) { for (const k of q.answerObj) { answer.push({ content: k.content, type: q.ansType, createdBy: userId, updatedBy: userId, createdAt: k.createdAt, }); } } const [listIdInquiry, listIdAnswer] = [ await models.TB_INQUIRY.bulkCreate(inquiry), await models.TB_ANSWER.bulkCreate(answer), ]; const inqMedia = []; const inqAnswer = []; if (isCdCm) { const inquiriesSort = listIdInquiry.sort((a, b) => (a.updatedAt > b.updatedAt ? 1 : -1)); const getGroupIds = []; const getElementFirst = []; inquiriesSort.forEach((l) => { if (!getGroupIds.includes(l.groupId)) { getGroupIds.push(l.groupId); getElementFirst.push(l.id); } }); if (getElementFirst.length && media.length) { getElementFirst.forEach((eId) => { media.forEach((m) => { inqMedia.push({ media: m.id, inquiry: eId, }); }); }); } } else { let mediaIndex = 0; let answerIndex = 0; question.forEach((q, inquiryIndex) => { q.mediaFile.forEach(() => { inqMedia.push({ media: media[mediaIndex].id, inquiry: listIdInquiry[inquiryIndex].id, }); mediaIndex += 1; }); q.answerObj.forEach(() => { inqAnswer.push({ inquiry: listIdInquiry[inquiryIndex].id, answer: listIdAnswer[answerIndex].id, confirm: false, createdBy: userId, updatedBy: userId, }); answerIndex += 1; }); }); await models.TB_INQ_ANS.bulkCreate(inqAnswer); } await models.TB_INQ_MEDIA.bulkCreate(inqMedia); const data = []; let inqRes = listIdInquiry.map((el) => el.get({ plain: true })); if (isCdCm) { inqRes = listIdInquiry.map((el) => el.get({ plain: true })); const getGroupIds = []; const getElementFirst = []; const getIdElementFirst = []; inqRes.forEach((l) => { if ( !getGroupIds.includes(l.groupId) && (userType === 'ADMIN' || l.receiver.includes(userType.toLowerCase())) ) { getGroupIds.push(l.groupId); getElementFirst.push(l); getIdElementFirst.push(l.id); } }); const removeElementFirst = inqRes.filter((l) => !getIdElementFirst.includes(l.id)); for (const inq of getElementFirst) { const { inquiryAnswers, inquiryMedia, receiver, ...rest } = inq; const inqGroup = []; if (removeElementFirst.length) { removeElementFirst.forEach((r) => { if ( inq.groupId === r.groupId && (userType === 'ADMIN' || r.receiver.includes(userType.toLowerCase())) ) { const obj = { id: r.id, content: r.content, groupId: r.groupId, inqType: r.inqType, receiver: r.receiver, }; inqGroup.push(obj); } }); } const dict = { ...rest, receiver, inqGroup, mediaFilesAnswer: [], mediaFile: [], answerObj: [...listIdAnswer], }; if (userType === 'ADMIN' || receiver.includes(userType.toLowerCase())) data.push(dict); } // } const inqResponse = inqRes.map((inq) => ({ ...inq, inqGroup: [], mediaFilesAnswer: [], mediaFile: [], process: 'pending', answerObj: [ ...listIdAnswer.filter( ({ id }) => inq.id === inqAnswer.find(({ answer }) => answer === id).inquiry, ), ], })); res .status(200) .json({ inqResponse: isCdCm ? data : inqResponse, message: msg.SAVE_INQUIRY_SUCCESSFULLY }); } } catch (error) { next(error); } }
Leave a Comment