diff --git a/.vscode/launch.json b/.vscode/launch.json index 6f1cd69..c56c2db 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -17,7 +17,7 @@ }, { "name": "Run npm nodemon (DEV)", - "command": "npm run debug", // > _logs/debug.log + "command": "npm run debug-npm-env", // > _logs/debug.log "request": "launch", "type": "node-terminal", "preLaunchTask": "killInspector", // <-- Add this line diff --git a/components/publisher/PublisherCard.js b/components/publisher/PublisherCard.js index f7fee63..04fa741 100644 --- a/components/publisher/PublisherCard.js +++ b/components/publisher/PublisherCard.js @@ -57,7 +57,8 @@ export default function PublisherCard({ publisher }) { return isCardVisible ? ( // className="block p-6 max-w-sm bg-white rounded-lg border border-gray-200 shadow-md hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700 mb-3"
+ ${!publisher.isactive ? "opacity-50 bg-gray-200 border-gray-300 text-gray-400" : (publisher.isImported ? "bg-orange-50" : (publisher.isTrained ? "bg-white" : "bg-red-50"))}`} + > Вестител - + {/* */} diff --git a/pages/cart/publishers/import.tsx b/pages/cart/publishers/import.tsx index 68b1a44..8d8ff2b 100644 --- a/pages/cart/publishers/import.tsx +++ b/pages/cart/publishers/import.tsx @@ -1,6 +1,6 @@ import { toast } from 'react-toastify'; import Layout from "../../../components/layout"; -import { Publisher, Availability, AvailabilityType, DayOfWeek, UserRole } from "@prisma/client"; +import { Publisher, Availability, AvailabilityType, DayOfWeek, UserRole, PublisherType } from "@prisma/client"; import ProtectedRoute from '../../../components/protectedRoute'; import axiosInstance from '../../../src/axiosSecure'; @@ -144,7 +144,7 @@ export default function ImportPage() { await new Promise(r => setTimeout(r, 100)); const row = rawData[i]; - var email, phone, names, dateOfInput, oldAvDeleted = false, isTrained = false, desiredShiftsPerMonth = 4, isActive = false; + var email, phone, names, dateOfInput, oldAvDeleted = false, isTrained = false, desiredShiftsPerMonth = 4, isActive = false, publisherType = PublisherType.Publisher; //const date = new Date(row[0]).toISOS{tring().slice(0, 10); if (mode.mainMode == MODE_PUBLISHERS1) { @@ -165,6 +165,7 @@ export default function ImportPage() { isTrained = row[headerRef.current.isTrainedIndex] !== ''; isActive = row[headerRef.current.isActiveIndex] == ''; desiredShiftsPerMonth = row[headerRef.current.desiredShiftsIndex] !== '' ? row[headerRef.current.desiredShiftsIndex] : 4; + publisherType = row[headerRef.current.pubTypeIndex]; } else { dateOfInput = common.excelSerialDateToDate(row[0]); @@ -179,10 +180,17 @@ export default function ImportPage() { let personId = ''; try { try { - const select = "&select=id,firstName,lastName,phone,isTrained,desiredShiftsPerMonth,isactive,availabilities"; + const select = "&select=id,firstName,lastName,phone,isTrained,desiredShiftsPerMonth,isactive,type,availabilities"; const responseByName = await axiosInstance.get(`/api/?action=findPublisher&filter=${names.join(' ')}${select}`); let existingPublisher = responseByName.data[0]; if (!existingPublisher) { + // Check if email is empty and generate a system one if needed + if (!email) { + const fullName = names.join(' ').toLowerCase(); // Assuming names is an array of [firstName, lastName] + email = fullName.replace(/\s+/g, '.'); // Replace spaces with dots + email += "";//@gmail.com? // Append a domain to make it a valid email format + } + // If no match by name, check by email const responseByEmail = await axiosInstance.get(`/api/?action=findPublisher&email=${email}${select}`); if (responseByEmail.data.length > 0) { @@ -199,7 +207,8 @@ export default function ImportPage() { // Create a flag to check if update is needed const updatedData = {}; personId = existingPublisher?.id; - let updateNeeded = false; + let fieldsToUpdateString = ''; + // Check for name update const fullName = names.join(' '); @@ -209,7 +218,7 @@ export default function ImportPage() { updatedData.firstName = names[0]; updatedData.lastName = names.slice(1).join(' '); data[i - mode.headerRow][4] = "name updated!"; - updateNeeded = true; + fieldsToUpdateString += `name, `; } else { data[i - mode.headerRow][4] = "existing"; } @@ -220,27 +229,33 @@ export default function ImportPage() { // Check for other updates const fieldsToUpdate = [ + { key: 'email', value: email }, { key: 'phone', value: phone }, { key: 'desiredShiftsPerMonth', value: desiredShiftsPerMonth, parse: parseInt }, { key: 'isTrained', value: isTrained }, - { key: 'isactive', value: isActive } + { key: 'isactive', value: isActive }, + { key: 'type', value: publisherType, parse: common.getPubTypeEnum } ]; fieldsToUpdate.forEach(({ key, value, parse }) => { - if (!existingPublisher[key] && value !== '' && value !== undefined) { + const newValue = parse ? parse(value) : value; + // Check if an update is needed: if the existing value is different or not set, and the new value is not empty/undefined + if ((existingPublisher[key] !== newValue && value !== '' && value !== undefined) + || (!existingPublisher.hasOwnProperty(key) && value !== '' && value !== undefined)) { updatedData[key] = parse ? parse(value) : value; - updateNeeded = true; + fieldsToUpdateString += `${key}, `; } }); // Update the record if needed and if MODE_PUBLISHERS1 (Import from List of Participants) - if (updateNeeded && (mode.publishers2Import || mode.mainMode == MODE_PUBLISHERS1)) { + if (fieldsToUpdateString.length > 0 && (mode.publishers2Import || mode.mainMode == MODE_PUBLISHERS1)) { try { await axiosInstance.put(`/api/data/publishers/${personId}`, updatedData); - common.logger.debug(`Updated publisher ${personId}`); - data[i - mode.headerRow][4] = "updated"; + common.logger.debug(`Updated publisher ${personId} - Fields Updated: ${fieldsToUpdateString}`); + data[i - mode.headerRow][4] = fieldsToUpdateString.substring(0, fieldsToUpdateString.length - 2) + + " updated"; } catch (error) { - console.error(`Failed to update publisher ${personId}`, error); + console.error(`Failed to update publisher ${personId} - Fields Attempted: ${fieldsToUpdateString}`, error); } }