The job_run_logs table tracks scheduled job orchestration, not individual worker jobs. Worker info (worker_id, worker_hostname) belongs on dispensary_crawl_jobs, not job_run_logs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
230 lines
7.6 KiB
JavaScript
230 lines
7.6 KiB
JavaScript
"use strict";
|
|
/**
|
|
* AZDHS Import Service
|
|
*
|
|
* Imports Arizona dispensaries from the main database's dispensaries table
|
|
* (which was populated from AZDHS data) into the isolated Dutchie AZ database.
|
|
*
|
|
* This establishes the canonical list of AZ dispensaries to match against Dutchie.
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.importAZDHSDispensaries = importAZDHSDispensaries;
|
|
exports.importFromJSON = importFromJSON;
|
|
exports.getImportStats = getImportStats;
|
|
const pg_1 = require("pg");
|
|
const connection_1 = require("../db/connection");
|
|
// Main database connection (source of AZDHS data)
|
|
const MAIN_DATABASE_URL = process.env.DATABASE_URL ||
|
|
'postgresql://dutchie:dutchie_local_pass@localhost:54320/dutchie_menus';
|
|
/**
|
|
* Create a temporary connection to the main database
|
|
*/
|
|
function getMainDBPool() {
|
|
return new pg_1.Pool({
|
|
connectionString: MAIN_DATABASE_URL,
|
|
max: 5,
|
|
idleTimeoutMillis: 30000,
|
|
connectionTimeoutMillis: 5000,
|
|
});
|
|
}
|
|
/**
|
|
* Fetch all AZ dispensaries from the main database
|
|
*/
|
|
async function fetchAZDHSDispensaries() {
|
|
const pool = getMainDBPool();
|
|
try {
|
|
const result = await pool.query(`
|
|
SELECT
|
|
id, azdhs_id, name, company_name, address, city, state, zip,
|
|
latitude, longitude, dba_name, phone, email, website,
|
|
google_rating, google_review_count, slug,
|
|
menu_provider, product_provider,
|
|
created_at, updated_at
|
|
FROM dispensaries
|
|
WHERE state = 'AZ'
|
|
ORDER BY id
|
|
`);
|
|
return result.rows;
|
|
}
|
|
finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
/**
|
|
* Import a single dispensary into the Dutchie AZ database
|
|
*/
|
|
async function importDispensary(disp) {
|
|
const result = await (0, connection_1.query)(`
|
|
INSERT INTO dispensaries (
|
|
platform, name, slug, city, state, postal_code, address,
|
|
latitude, longitude, is_delivery, is_pickup, raw_metadata, updated_at
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5, $6, $7,
|
|
$8, $9, $10, $11, $12, NOW()
|
|
)
|
|
ON CONFLICT (platform, slug, city, state) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
postal_code = EXCLUDED.postal_code,
|
|
address = EXCLUDED.address,
|
|
latitude = EXCLUDED.latitude,
|
|
longitude = EXCLUDED.longitude,
|
|
raw_metadata = EXCLUDED.raw_metadata,
|
|
updated_at = NOW()
|
|
RETURNING id
|
|
`, [
|
|
'dutchie', // Will be updated when Dutchie match is found
|
|
disp.dba_name || disp.name,
|
|
disp.slug,
|
|
disp.city,
|
|
disp.state,
|
|
disp.zip,
|
|
disp.address,
|
|
disp.latitude,
|
|
disp.longitude,
|
|
false, // is_delivery - unknown
|
|
true, // is_pickup - assume true
|
|
JSON.stringify({
|
|
azdhs_id: disp.azdhs_id,
|
|
main_db_id: disp.id,
|
|
company_name: disp.company_name,
|
|
phone: disp.phone,
|
|
email: disp.email,
|
|
website: disp.website,
|
|
google_rating: disp.google_rating,
|
|
google_review_count: disp.google_review_count,
|
|
menu_provider: disp.menu_provider,
|
|
product_provider: disp.product_provider,
|
|
}),
|
|
]);
|
|
return result.rows[0].id;
|
|
}
|
|
/**
|
|
* Import all AZDHS dispensaries into the Dutchie AZ database
|
|
*/
|
|
async function importAZDHSDispensaries() {
|
|
console.log('[AZDHS Import] Starting import from main database...');
|
|
const result = {
|
|
total: 0,
|
|
imported: 0,
|
|
skipped: 0,
|
|
errors: [],
|
|
};
|
|
try {
|
|
const dispensaries = await fetchAZDHSDispensaries();
|
|
result.total = dispensaries.length;
|
|
console.log(`[AZDHS Import] Found ${dispensaries.length} AZ dispensaries in main DB`);
|
|
for (const disp of dispensaries) {
|
|
try {
|
|
const id = await importDispensary(disp);
|
|
result.imported++;
|
|
console.log(`[AZDHS Import] Imported: ${disp.name} (${disp.city}) -> id=${id}`);
|
|
}
|
|
catch (error) {
|
|
if (error.message.includes('duplicate')) {
|
|
result.skipped++;
|
|
}
|
|
else {
|
|
result.errors.push(`${disp.name}: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
result.errors.push(`Failed to fetch from main DB: ${error.message}`);
|
|
}
|
|
console.log(`[AZDHS Import] Complete: ${result.imported} imported, ${result.skipped} skipped, ${result.errors.length} errors`);
|
|
return result;
|
|
}
|
|
/**
|
|
* Import dispensaries from JSON file (backup export)
|
|
*/
|
|
async function importFromJSON(jsonPath) {
|
|
console.log(`[AZDHS Import] Importing from JSON: ${jsonPath}`);
|
|
const result = {
|
|
total: 0,
|
|
imported: 0,
|
|
skipped: 0,
|
|
errors: [],
|
|
};
|
|
try {
|
|
const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
|
|
const data = await fs.readFile(jsonPath, 'utf-8');
|
|
const dispensaries = JSON.parse(data);
|
|
result.total = dispensaries.length;
|
|
console.log(`[AZDHS Import] Found ${dispensaries.length} dispensaries in JSON file`);
|
|
for (const disp of dispensaries) {
|
|
try {
|
|
const id = await importDispensary(disp);
|
|
result.imported++;
|
|
}
|
|
catch (error) {
|
|
if (error.message.includes('duplicate')) {
|
|
result.skipped++;
|
|
}
|
|
else {
|
|
result.errors.push(`${disp.name}: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
result.errors.push(`Failed to read JSON file: ${error.message}`);
|
|
}
|
|
console.log(`[AZDHS Import] Complete: ${result.imported} imported, ${result.skipped} skipped`);
|
|
return result;
|
|
}
|
|
/**
|
|
* Get import statistics
|
|
*/
|
|
async function getImportStats() {
|
|
const { rows } = await (0, connection_1.query)(`
|
|
SELECT
|
|
COUNT(*) as total,
|
|
COUNT(platform_dispensary_id) as with_platform_id,
|
|
COUNT(*) - COUNT(platform_dispensary_id) as without_platform_id,
|
|
MAX(updated_at) as last_updated
|
|
FROM dispensaries
|
|
WHERE state = 'AZ'
|
|
`);
|
|
const stats = rows[0];
|
|
return {
|
|
totalDispensaries: parseInt(stats.total, 10),
|
|
withPlatformIds: parseInt(stats.with_platform_id, 10),
|
|
withoutPlatformIds: parseInt(stats.without_platform_id, 10),
|
|
lastImportedAt: stats.last_updated,
|
|
};
|
|
}
|