All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit introduces a new function, `syncModelsWithScheduler`, to synchronize database schemas with the farm control scheduler. It also adds scheduled properties and event handling for invoices, allowing for automatic state updates based on due dates. Additionally, minor refactoring is performed in the sales listings routes and services to improve code readability and maintainability. The changes enhance the application's data synchronization and invoice management capabilities.
102 lines
3.1 KiB
JavaScript
102 lines
3.1 KiB
JavaScript
import { diffLines } from 'diff';
|
|
import fs from 'fs/promises';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
async function syncModelsWithWS() {
|
|
const sourceDir = path.resolve(__dirname, 'src/database/schemas');
|
|
const targetDir = path.resolve(__dirname, '../farmcontrol-ws/src/database/schemas');
|
|
|
|
console.log(`Syncing schemas from ${sourceDir} to ${targetDir}...`);
|
|
|
|
const stats = { copied: 0, added: 0, skipped: 0 };
|
|
|
|
try {
|
|
await syncDirectory(sourceDir, targetDir, sourceDir, stats);
|
|
console.log(
|
|
`✅ Schema sync completed successfully! (${stats.added} added, ${stats.copied} updated, ${stats.skipped} unchanged)`
|
|
);
|
|
} catch (error) {
|
|
console.error('❌ Error syncing schemas:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
async function syncModelsWithScheduler() {
|
|
const sourceDir = path.resolve(__dirname, 'src/database/schemas');
|
|
const targetDir = path.resolve(__dirname, '../farmcontrol-scheduler/src/database/schemas');
|
|
|
|
console.log(`Syncing schemas from ${sourceDir} to ${targetDir}...`);
|
|
|
|
const stats = { copied: 0, added: 0, skipped: 0 };
|
|
|
|
try {
|
|
await syncDirectory(sourceDir, targetDir, sourceDir, stats);
|
|
console.log(
|
|
`✅ Schema sync completed successfully! (${stats.added} added, ${stats.copied} updated, ${stats.skipped} unchanged)`
|
|
);
|
|
} catch (error) {
|
|
console.error('❌ Error syncing schemas:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function fileHasChanges(oldContent, newContent) {
|
|
return diffLines(oldContent, newContent).some((part) => part.added || part.removed);
|
|
}
|
|
|
|
async function readFileIfExists(filePath) {
|
|
try {
|
|
return await fs.readFile(filePath, 'utf8');
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function syncDirectory(source, target, rootSource, stats) {
|
|
try {
|
|
await fs.access(target);
|
|
} catch {
|
|
await fs.mkdir(target, { recursive: true });
|
|
}
|
|
|
|
const items = await fs.readdir(source, { withFileTypes: true });
|
|
|
|
for (const item of items) {
|
|
const sourcePath = path.join(source, item.name);
|
|
const targetPath = path.join(target, item.name);
|
|
|
|
if (item.isDirectory()) {
|
|
await syncDirectory(sourcePath, targetPath, rootSource, stats);
|
|
} else if (item.isFile()) {
|
|
const sourceContent = await fs.readFile(sourcePath, 'utf8');
|
|
const targetContent = await readFileIfExists(targetPath);
|
|
const relativePath = path.relative(rootSource, sourcePath);
|
|
|
|
if (targetContent === null) {
|
|
await fs.writeFile(targetPath, sourceContent);
|
|
stats.added += 1;
|
|
console.log(` + Added: ${relativePath}`);
|
|
} else if (fileHasChanges(targetContent, sourceContent)) {
|
|
await fs.writeFile(targetPath, sourceContent);
|
|
stats.copied += 1;
|
|
console.log(` ✓ Updated: ${relativePath}`);
|
|
} else {
|
|
stats.skipped += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run the sync function when executed directly
|
|
syncModelsWithWS();
|
|
syncModelsWithScheduler();
|
|
|
|
export { syncModelsWithWS, syncModelsWithScheduler };
|