|
| 1 | +import { Job } from "bullmq" |
| 2 | +import { countryCodes, dbServers, EngineType } from "../config/enums" |
| 3 | +import { ContextType } from "../libs/logger" |
| 4 | +import { jsonOrStringForDb, jsonOrStringToJson, stringOrNullForDb, stringToHash } from "../utils" |
| 5 | +import _ from "lodash" |
| 6 | +import { sources } from "../sites/sources" |
| 7 | +import items from "./../../pharmacyItems.json" |
| 8 | +import connections from "./../../brandConnections.json" |
| 9 | + |
| 10 | +type BrandsMapping = { |
| 11 | + [key: string]: string[] |
| 12 | +} |
| 13 | + |
| 14 | +export async function getBrandsMapping(): Promise<BrandsMapping> { |
| 15 | + // const query = ` |
| 16 | + // SELECT |
| 17 | + // LOWER(p1.manufacturer) manufacturer_p1 |
| 18 | + // , LOWER(GROUP_CONCAT(DISTINCT p2.manufacturer ORDER BY p2.manufacturer SEPARATOR ';')) AS manufacturers_p2 |
| 19 | + // FROM |
| 20 | + // property_matchingvalidation v |
| 21 | + // INNER JOIN |
| 22 | + // property_pharmacy p1 ON v.m_source = p1.source |
| 23 | + // AND v.m_source_id = p1.source_id |
| 24 | + // AND v.m_country_code = p1.country_code |
| 25 | + // AND p1.newest = true |
| 26 | + // INNER JOIN |
| 27 | + // property_pharmacy p2 ON v.c_source = p2.source |
| 28 | + // AND v.c_source_id = p2.source_id |
| 29 | + // AND v.c_country_code = p2.country_code |
| 30 | + // AND p2.newest = true |
| 31 | + // WHERE |
| 32 | + // v.m_source = 'AZT' |
| 33 | + // AND v.engine_type = '${EngineType.Barcode}' |
| 34 | + // and p1.manufacturer is not null |
| 35 | + // and p2.manufacturer is not null |
| 36 | + // and p1.manufacturer not in ('kita', 'nera', 'cits') |
| 37 | + // and p2.manufacturer not in ('kita', 'nera', 'cits') |
| 38 | + // GROUP BY |
| 39 | + // p1.manufacturer |
| 40 | + // ` |
| 41 | + // const brandConnections = await executeQueryAndGetResponse(dbServers.pharmacy, query) |
| 42 | + // For this test day purposes exported the necessary object |
| 43 | + const brandConnections = connections |
| 44 | + |
| 45 | + const getRelatedBrands = (map: Map<string, Set<string>>, brand: string): Set<string> => { |
| 46 | + const relatedBrands = new Set<string>() |
| 47 | + const queue = [brand] |
| 48 | + while (queue.length > 0) { |
| 49 | + const current = queue.pop()! |
| 50 | + if (map.has(current)) { |
| 51 | + const brands = map.get(current)! |
| 52 | + for (const b of brands) { |
| 53 | + if (!relatedBrands.has(b)) { |
| 54 | + relatedBrands.add(b) |
| 55 | + queue.push(b) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return relatedBrands |
| 61 | + } |
| 62 | + |
| 63 | + // Create a map to track brand relationships |
| 64 | + const brandMap = new Map<string, Set<string>>() |
| 65 | + |
| 66 | + brandConnections.forEach(({ manufacturer_p1, manufacturers_p2 }) => { |
| 67 | + const brand1 = manufacturer_p1.toLowerCase() |
| 68 | + const brands2 = manufacturers_p2.toLowerCase() |
| 69 | + const brand2Array = brands2.split(";").map((b) => b.trim()) |
| 70 | + if (!brandMap.has(brand1)) { |
| 71 | + brandMap.set(brand1, new Set()) |
| 72 | + } |
| 73 | + brand2Array.forEach((brand2) => { |
| 74 | + if (!brandMap.has(brand2)) { |
| 75 | + brandMap.set(brand2, new Set()) |
| 76 | + } |
| 77 | + brandMap.get(brand1)!.add(brand2) |
| 78 | + brandMap.get(brand2)!.add(brand1) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + // Build the final flat map |
| 83 | + const flatMap = new Map<string, Set<string>>() |
| 84 | + |
| 85 | + brandMap.forEach((_, brand) => { |
| 86 | + const relatedBrands = getRelatedBrands(brandMap, brand) |
| 87 | + flatMap.set(brand, relatedBrands) |
| 88 | + }) |
| 89 | + |
| 90 | + // Convert the flat map to an object for easier usage |
| 91 | + const flatMapObject: Record<string, string[]> = {} |
| 92 | + |
| 93 | + flatMap.forEach((relatedBrands, brand) => { |
| 94 | + flatMapObject[brand] = Array.from(relatedBrands) |
| 95 | + }) |
| 96 | + |
| 97 | + return flatMapObject |
| 98 | +} |
| 99 | + |
| 100 | +async function getPharmacyItems(countryCode: countryCodes, source: sources, versionKey: string, mustExist = true) { |
| 101 | + // let query = ` |
| 102 | + // SELECT |
| 103 | + // p.url, p.removed_timestamp, p.title, p.source_id |
| 104 | + // , p.manufacturer |
| 105 | + // , map.source_id m_id |
| 106 | + // , map.source |
| 107 | + // , map.country_code |
| 108 | + // , map.meta |
| 109 | + // FROM |
| 110 | + // property_pharmacy p |
| 111 | + // left join pharmacy_mapping map on p.source_id = map.source_id and p.source = map.source and p.country_code = map.country_code |
| 112 | + // WHERE |
| 113 | + // p.newest = TRUE |
| 114 | + // and p.country_code = '${countryCode}' |
| 115 | + // and p.source = '${source}' |
| 116 | + // and p.removed_timestamp is null |
| 117 | + // and (p.manufacturer is null or p.manufacturer in ('nera', 'kita', 'cits')) |
| 118 | + // ORDER BY p.removed_timestamp IS NULL DESC, p.removed_timestamp DESC |
| 119 | + // ` |
| 120 | + // let products = await executeQueryAndGetResponse(dbServers.pharmacy, query) |
| 121 | + // for (let product of products) { |
| 122 | + // product.meta = jsonOrStringToJson(product.meta) |
| 123 | + // } |
| 124 | + |
| 125 | + // let finalProducts = products.filter((product) => (!mustExist || product.m_id) && !product.meta[versionKey]) |
| 126 | + const finalProducts = items |
| 127 | + |
| 128 | + return finalProducts |
| 129 | +} |
| 130 | + |
| 131 | +export function checkBrandIsSeparateTerm(input: string, brand: string): boolean { |
| 132 | + // Escape any special characters in the brand name for use in a regular expression |
| 133 | + const escapedBrand = brand.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") |
| 134 | + |
| 135 | + // Check if the brand is at the beginning or end of the string |
| 136 | + const atBeginningOrEnd = new RegExp( |
| 137 | + `^(?:${escapedBrand}\\s|.*\\s${escapedBrand}\\s.*|.*\\s${escapedBrand})$`, |
| 138 | + "i" |
| 139 | + ).test(input) |
| 140 | + |
| 141 | + // Check if the brand is a separate term in the string |
| 142 | + const separateTerm = new RegExp(`\\b${escapedBrand}\\b`, "i").test(input) |
| 143 | + |
| 144 | + // The brand should be at the beginning, end, or a separate term |
| 145 | + return atBeginningOrEnd || separateTerm |
| 146 | +} |
| 147 | + |
| 148 | +export async function assignBrandIfKnown(countryCode: countryCodes, source: sources, job?: Job) { |
| 149 | + const context = { scope: "assignBrandIfKnown" } as ContextType |
| 150 | + |
| 151 | + const brandsMapping = await getBrandsMapping() |
| 152 | + |
| 153 | + const versionKey = "assignBrandIfKnown" |
| 154 | + let products = await getPharmacyItems(countryCode, source, versionKey, false) |
| 155 | + let counter = 0 |
| 156 | + for (let product of products) { |
| 157 | + counter++ |
| 158 | + |
| 159 | + if (product.m_id) { |
| 160 | + // Already exists in the mapping table, probably no need to update |
| 161 | + continue |
| 162 | + } |
| 163 | + |
| 164 | + let matchedBrands = [] |
| 165 | + for (const brandKey in brandsMapping) { |
| 166 | + const relatedBrands = brandsMapping[brandKey] |
| 167 | + for (const brand of relatedBrands) { |
| 168 | + if (matchedBrands.includes(brand)) { |
| 169 | + continue |
| 170 | + } |
| 171 | + const isBrandMatch = checkBrandIsSeparateTerm(product.title, brand) |
| 172 | + if (isBrandMatch) { |
| 173 | + matchedBrands.push(brand) |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + console.log(`${product.title} -> ${_.uniq(matchedBrands)}`) |
| 178 | + const sourceId = product.source_id |
| 179 | + const meta = { matchedBrands } |
| 180 | + const brand = matchedBrands.length ? matchedBrands[0] : null |
| 181 | + |
| 182 | + const key = `${source}_${countryCode}_${sourceId}` |
| 183 | + const uuid = stringToHash(key) |
| 184 | + |
| 185 | + // Then brand is inserted into product mapping table |
| 186 | + } |
| 187 | +} |
0 commit comments