diff --git a/apps/api/src/migrations/shift-environment-key/shift-environment-key.migration.ts b/apps/api/src/migrations/shift-environment-key/shift-environment-key.migration.ts index 4b5ca451..101523a2 100644 --- a/apps/api/src/migrations/shift-environment-key/shift-environment-key.migration.ts +++ b/apps/api/src/migrations/shift-environment-key/shift-environment-key.migration.ts @@ -1,8 +1,8 @@ import '../../config'; import { AppModule } from '../../app.module'; import { NestFactory } from '@nestjs/core'; -import { EnvironmentRepository } from '@impler/dal'; import { UserRolesEnum } from '@impler/shared'; +import { EnvironmentRepository, Environment } from '@impler/dal'; export async function run() { console.log('Start migration - moving key to root and adding role to apiKeys'); @@ -16,6 +16,7 @@ export async function run() { const environments = await environmentRepository.find({}); + const batchOperations = []; for (const environment of environments) { if (environment.apiKeys && environment.apiKeys.length > 0) { const firstApiKey = environment.apiKeys[0]; @@ -34,26 +35,30 @@ export async function run() { continue; } - try { - // TODO: optimize update query in loop - await environmentRepository.update( - { _id: environment._id }, - { + batchOperations.push({ + updateOne: { + filter: { _id: environment._id }, + update: { $set: { key: key, apiKeys: updatedApiKeys, }, - } - ); - } catch (error) { - console.error(`Error updating environment ${environment._id}:`, error); - } + }, + }, + }); } + } - console.log('End migration - key moved to root and role added to apiKeys'); - - app.close(); - process.exit(0); + if (batchOperations.length > 0) { + await Environment.bulkWrite(batchOperations, { + ordered: false, + }); + batchOperations.length = 0; } - run(); + + console.log('End migration - key moved to root and role added to apiKeys'); + + app.close(); + process.exit(0); } +run(); diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 13a05029..67a81ef2 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -26,8 +26,7 @@ export function middleware(request: NextRequest) { else return; else if ([ROUTES.SIGNIN, ROUTES.OTP_VERIFY, ROUTES.SIGNUP_ONBOARDING, ROUTES.SIGNUP].includes(path)) return NextResponse.redirect(new URL(ROUTES.HOME, request.url)); - else if ((path == ROUTES.SIGNUP, [ROUTES.SIGNUP] && !cookie)) - return NextResponse.redirect(new URL(ROUTES.SIGNUP, request.url)); + else if (path == ROUTES.SIGNUP && !cookie) return NextResponse.redirect(new URL(ROUTES.SIGNUP, request.url)); // else if (path !== ROUTES.HOME) }