@7365admin1/module-hygiene 4.28.0 → 4.28.1-staging.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1608,7 +1608,7 @@ function useUnitRepository() {
1608
1608
  function useAreaService() {
1609
1609
  const { createArea: _createArea, getAreasForChecklist } = useAreaRepo();
1610
1610
  const { generateAreaExcel } = useAreaExportService();
1611
- const { getUnits: _getUnits } = useUnitRepository();
1611
+ const { createUnit: _createUnit, getUnits: _getUnits } = useUnitRepository();
1612
1612
  async function importArea({
1613
1613
  dataJson,
1614
1614
  site,
@@ -1690,23 +1690,40 @@ function useAreaService() {
1690
1690
  areaData.set = setNumber;
1691
1691
  }
1692
1692
  }
1693
- if (row.UNITS && availableUnits.length > 0) {
1693
+ if (row.UNITS) {
1694
1694
  const unitNames = String(row.UNITS).split(",").map((u) => u.trim()).filter((u) => u);
1695
1695
  if (unitNames.length > 0) {
1696
1696
  const areaUnits = [];
1697
1697
  for (const unitName of unitNames) {
1698
- const foundUnit = availableUnits.find(
1698
+ let foundUnit = availableUnits.find(
1699
1699
  (u) => u.name.toLowerCase() === unitName.toLowerCase()
1700
1700
  );
1701
+ if (!foundUnit) {
1702
+ try {
1703
+ const insertedUnitId = await _createUnit({
1704
+ name: unitName,
1705
+ site,
1706
+ serviceType
1707
+ });
1708
+ foundUnit = {
1709
+ _id: insertedUnitId,
1710
+ name: unitName
1711
+ };
1712
+ availableUnits.push(foundUnit);
1713
+ logger8.info(
1714
+ `Successfully created missing unit "${unitName}" for area "${areaName}"`
1715
+ );
1716
+ } catch (error) {
1717
+ logger8.warn(
1718
+ `Unit "${unitName}" could not be created for area "${areaName}": ${error.message}`
1719
+ );
1720
+ }
1721
+ }
1701
1722
  if (foundUnit) {
1702
1723
  areaUnits.push({
1703
1724
  unit: foundUnit._id,
1704
1725
  name: foundUnit.name
1705
1726
  });
1706
- } else {
1707
- logger8.warn(
1708
- `Unit "${unitName}" not found in site for area "${areaName}"`
1709
- );
1710
1727
  }
1711
1728
  }
1712
1729
  if (areaUnits.length > 0) {
@@ -2098,6 +2115,13 @@ function useUnitService() {
2098
2115
  updateUnit: _updateUnit,
2099
2116
  deleteUnit: _deleteUnit
2100
2117
  } = useUnitRepository();
2118
+ function getUnitNamesFromRow(row) {
2119
+ const unitValue = row?.UNIT ?? row?.UNITS;
2120
+ if (!unitValue) {
2121
+ return [];
2122
+ }
2123
+ return String(unitValue).split(",").map((unit) => unit.trim()).filter((unit) => unit && !unit.startsWith("Sample:"));
2124
+ }
2101
2125
  async function importUnit({
2102
2126
  dataJson,
2103
2127
  site,
@@ -2123,36 +2147,35 @@ function useUnitService() {
2123
2147
  try {
2124
2148
  for (let i = 0; i < dataArray.length; i++) {
2125
2149
  const row = dataArray[i];
2126
- if (!row?.UNIT) {
2127
- logger11.warn(`Skipping row ${i + 1} with missing UNIT:`, row);
2128
- skippedRows.push(i + 1);
2129
- continue;
2130
- }
2131
- const unitName = String(row.UNIT).trim();
2132
- if (!unitName) {
2133
- logger11.warn(`Skipping row ${i + 1} with empty unit name`);
2134
- skippedRows.push(i + 1);
2135
- continue;
2136
- }
2137
- if (unitName.startsWith("Sample:")) {
2138
- logger11.warn(`Skipping row ${i + 1} with sample unit: ${unitName}`);
2150
+ const unitNames = getUnitNamesFromRow(row);
2151
+ if (unitNames.length === 0) {
2152
+ logger11.warn(`Skipping row ${i + 1} with missing UNIT/UNITS:`, row);
2139
2153
  skippedRows.push(i + 1);
2140
2154
  continue;
2141
2155
  }
2142
- try {
2143
- const insertedId = await _createUnit({
2144
- name: unitName,
2145
- site,
2146
- serviceType
2147
- });
2148
- insertedUnitIds.push(insertedId);
2149
- logger11.info(`Successfully created unit: ${unitName}`);
2150
- } catch (error) {
2151
- logger11.error(`Error creating unit "${unitName}": ${error.message}`);
2152
- if (error.message.includes("Unit already exists")) {
2153
- duplicateUnits.push(unitName);
2154
- } else {
2155
- failedUnits.push(unitName);
2156
+ const uniqueUnitNames = Array.from(
2157
+ new Set(unitNames.map((unitName) => unitName.toLowerCase()))
2158
+ ).map(
2159
+ (lowerUnitName) => unitNames.find(
2160
+ (unitName) => unitName.toLowerCase() === lowerUnitName
2161
+ )
2162
+ );
2163
+ for (const unitName of uniqueUnitNames) {
2164
+ try {
2165
+ const insertedId = await _createUnit({
2166
+ name: unitName,
2167
+ site,
2168
+ serviceType
2169
+ });
2170
+ insertedUnitIds.push(insertedId);
2171
+ logger11.info(`Successfully created unit: ${unitName}`);
2172
+ } catch (error) {
2173
+ logger11.error(`Error creating unit "${unitName}": ${error.message}`);
2174
+ if (error.message.includes("Unit already exists")) {
2175
+ duplicateUnits.push(unitName);
2176
+ } else {
2177
+ failedUnits.push(unitName);
2178
+ }
2156
2179
  }
2157
2180
  }
2158
2181
  }