import now imports gender

This commit is contained in:
Dobromir Popov
2024-04-19 17:02:36 +03:00
parent d56dbde234
commit 54472ad8f2

View File

@ -51,7 +51,8 @@ export default function ImportPage() {
desiredShiftsIndex: -1, desiredShiftsIndex: -1,
dataStartIndex: -1, dataStartIndex: -1,
isActiveIndex: -1, isActiveIndex: -1,
pubTypeIndex: -1 pubTypeIndex: -1,
gender: -1
}); });
const handleFile = (e) => { const handleFile = (e) => {
@ -111,6 +112,7 @@ export default function ImportPage() {
headerRef.current.desiredShiftsIndex = header.indexOf('Желан брой участия'); headerRef.current.desiredShiftsIndex = header.indexOf('Желан брой участия');
headerRef.current.isActiveIndex = header.indexOf("Неактивен"); headerRef.current.isActiveIndex = header.indexOf("Неактивен");
headerRef.current.pubTypeIndex = header.indexOf("Назначение"); headerRef.current.pubTypeIndex = header.indexOf("Назначение");
headerRef.current.gender = header.indexOf("Пол");
const filteredData = sheetData.slice(headerRef.current.dataStartIndex).map((row) => { const filteredData = sheetData.slice(headerRef.current.dataStartIndex).map((row) => {
let date; let date;
@ -147,12 +149,16 @@ export default function ImportPage() {
let isOld = false; let isOld = false;
const row = rawData[i]; const row = rawData[i];
let email, phone, names, dateOfInput, oldAvDeleted = false, isTrained = false, desiredShiftsPerMonth = 4, isActive = true, publisherType = PublisherType.Publisher; let email, phone, names, dateOfInput, oldAvDeleted = false,
//const date = new Date(row[0]).toISOS{tring().slice(0, 10); isTrained = false, desiredShiftsPerMonth = 4, isActive = true,
publisherType = PublisherType.Publisher,
isMale = 0
;
//ToDo: structure all vars above as single object:
if (mode.mainMode == MODE_PUBLISHERS1) { if (mode.mainMode == MODE_PUBLISHERS1) {
email = row[headerRef.current.emailIndex]; email = row[headerRef.current.emailIndex];
phone = row[headerRef.current.phoneIndex].toString().trim(); // Trim whitespace phone = row[headerRef.current.phoneIndex].toString().trim(); // Trim whitespace
// Remove any non-digit characters, except for the leading + // Remove any non-digit characters, except for the leading +
//phone = phone.replace(/(?!^\+)\D/g, ''); //phone = phone.replace(/(?!^\+)\D/g, '');
@ -165,11 +171,11 @@ export default function ImportPage() {
names = row[headerRef.current.nameIndex].normalize('NFC').split(/[ ]+/); names = row[headerRef.current.nameIndex].normalize('NFC').split(/[ ]+/);
dateOfInput = importDate.value || new Date().toISOString(); dateOfInput = importDate.value || new Date().toISOString();
// not empty == true // not empty == true
isTrained = row[headerRef.current.isTrainedIndex] !== ''; isTrained = row[headerRef.current.isTrainedIndex] !== '';
isActive = row[headerRef.current.isActiveIndex] == ''; isActive = row[headerRef.current.isActiveIndex] == '';
desiredShiftsPerMonth = row[headerRef.current.desiredShiftsIndex] !== '' ? row[headerRef.current.desiredShiftsIndex] : 4; desiredShiftsPerMonth = row[headerRef.current.desiredShiftsIndex] !== '' ? row[headerRef.current.desiredShiftsIndex] : 4;
publisherType = row[headerRef.current.pubTypeIndex]; publisherType = row[headerRef.current.pubTypeIndex];
isMale = row[headerRef.current.gender].trim().toLowerCase() === 'брат';
} }
else { else {
dateOfInput = common.excelSerialDateToDate(row[0]); dateOfInput = common.excelSerialDateToDate(row[0]);
@ -201,7 +207,7 @@ export default function ImportPage() {
let personNames = names.join(' '); let personNames = names.join(' ');
try { try {
try { try {
const select = "&select=id,firstName,lastName,email,phone,isTrained,desiredShiftsPerMonth,isActive,type,availabilities"; const select = "&select=id,firstName,lastName,email,phone,isTrained,desiredShiftsPerMonth,isActive,isMale,type,availabilities";
const responseByName = await axiosInstance.get(`/api/?action=findPublisher&filter=${names.join(' ')}${select}`); const responseByName = await axiosInstance.get(`/api/?action=findPublisher&filter=${names.join(' ')}${select}`);
let existingPublisher = responseByName.data[0]; let existingPublisher = responseByName.data[0];
if (!existingPublisher) { if (!existingPublisher) {
@ -244,11 +250,8 @@ export default function ImportPage() {
} else { } else {
data[i - mode.headerRow][4] = "existing"; data[i - mode.headerRow][4] = "existing";
} }
// Log existing publisher
common.logger.debug(`Existing publisher '${[existingPublisher.firstName, existingPublisher.lastName].join(' ')}' found for ${email} (ID:${personId})`); common.logger.debug(`Existing publisher '${[existingPublisher.firstName, existingPublisher.lastName].join(' ')}' found for ${email} (ID:${personId})`);
// Check for other updates // Check for other updates
const fieldsToUpdate = [ const fieldsToUpdate = [
{ key: 'email', value: email }, { key: 'email', value: email },
@ -256,6 +259,7 @@ export default function ImportPage() {
{ key: 'desiredShiftsPerMonth', value: desiredShiftsPerMonth, parse: parseInt }, { key: 'desiredShiftsPerMonth', value: desiredShiftsPerMonth, parse: parseInt },
{ key: 'isTrained', value: isTrained }, { key: 'isTrained', value: isTrained },
{ key: 'isActive', value: isActive }, { key: 'isActive', value: isActive },
{ key: "isMale", value: isMale },
{ key: 'type', value: publisherType, parse: common.getPubTypeEnum } { key: 'type', value: publisherType, parse: common.getPubTypeEnum }
]; ];
@ -277,6 +281,7 @@ export default function ImportPage() {
data[i - mode.headerRow][4] = fieldsToUpdateString.substring(0, fieldsToUpdateString.length - 2) data[i - mode.headerRow][4] = fieldsToUpdateString.substring(0, fieldsToUpdateString.length - 2)
+ " updated"; + " updated";
} catch (error) { } catch (error) {
data[i - mode.headerRow][4] = "error updating!";
console.error(`Failed to update publisher ${personId} - Fields Attempted: ${fieldsToUpdateString}`, error); console.error(`Failed to update publisher ${personId} - Fields Attempted: ${fieldsToUpdateString}`, error);
} }
} }
@ -309,6 +314,7 @@ export default function ImportPage() {
firstName: firstname, firstName: firstname,
lastName: names[names.length - 1], lastName: names[names.length - 1],
isActive: isActive, isActive: isActive,
isMale: isMale,
isTrained, isTrained,
desiredShiftsPerMonth desiredShiftsPerMonth
}); });