Enhance Button and CreateBuildModal components; add title prop to Button for accessibility, refactor CreateBuildModal to manage multiple builds selection, improve loading states, and streamline API interactions for better user experience.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Миграция: создание связей CompanyBuild из старых Build с companyId
|
||||
*
|
||||
* Запуск: npx ts-node --esm scripts/migrate-builds-to-company-build.ts
|
||||
* (из папки server, с загруженным .env)
|
||||
*/
|
||||
import "dotenv/config";
|
||||
import mongoose from "mongoose";
|
||||
import Build from "../src/models/Build.js";
|
||||
import CompanyBuild from "../src/models/CompanyBuild.js";
|
||||
|
||||
async function migrate() {
|
||||
await mongoose.connect(process.env.MONGO_URI!, { dbName: "crm_stream" });
|
||||
console.log("MongoDB connected");
|
||||
|
||||
const db = mongoose.connection.db;
|
||||
if (!db) throw new Error("No database connection");
|
||||
|
||||
const buildsCollection = db.collection("builds");
|
||||
const buildsWithCompany = await buildsCollection
|
||||
.find({ companyId: { $exists: true, $ne: null } })
|
||||
.toArray();
|
||||
|
||||
console.log(`Найдено ${buildsWithCompany.length} сборок с companyId`);
|
||||
|
||||
let created = 0;
|
||||
let skipped = 0;
|
||||
|
||||
for (const build of buildsWithCompany) {
|
||||
const companyId = build.companyId;
|
||||
const buildId = build._id;
|
||||
|
||||
const existing = await CompanyBuild.findOne({
|
||||
companyId,
|
||||
buildId,
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
await CompanyBuild.create({
|
||||
companyId: new mongoose.Types.ObjectId(companyId.toString()),
|
||||
buildId: new mongoose.Types.ObjectId(buildId.toString()),
|
||||
});
|
||||
created++;
|
||||
console.log(` Создана связь: company ${companyId} -> build ${buildId}`);
|
||||
}
|
||||
|
||||
console.log(`\nГотово. Создано: ${created}, пропущено (уже есть): ${skipped}`);
|
||||
|
||||
// Удаляем companyId из документов Build (опционально, для консистентности)
|
||||
const unsetResult = await buildsCollection.updateMany(
|
||||
{ companyId: { $exists: true } },
|
||||
{ $unset: { companyId: "" } }
|
||||
);
|
||||
console.log(`Удалено поле companyId из ${unsetResult.modifiedCount} документов Build`);
|
||||
|
||||
await mongoose.disconnect();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
migrate().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user