Skip to content

Commit

Permalink
fix: Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik committed Oct 18, 2024
1 parent c7a47ed commit d19c737
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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];
Expand All @@ -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();
3 changes: 1 addition & 2 deletions apps/web/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit d19c737

Please sign in to comment.