Skip to content

Commit

Permalink
Merge pull request #1571 from contentstack/fix/dx-1472-set-rate-limit…
Browse files Browse the repository at this point in the history
…-org-issue

dx | 1472 set rate limit org issue
  • Loading branch information
harshithad0703 authored Sep 19, 2024
2 parents caa5343 + 139a6d5 commit 4c73e13
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class SetRateLimitCommand extends BaseCommand<typeof SetRateLimit

const { flags } = await this.parse(SetRateLimitCommand);
let { org, utilize, 'limit-name': limitName } = flags;
const config: SetRateLimitConfig = { org: '', limitName: limitNamesConfig };
const config: SetRateLimitConfig = { org: '', limitName: limitNamesConfig, host: this.cmaHost };

if (!org) {
org = await askOrgID();
Expand Down Expand Up @@ -82,7 +82,8 @@ export default class SetRateLimitCommand extends BaseCommand<typeof SetRateLimit
try {
await limitHandler.setRateLimit(config);
} catch (error) {
cliux.error(`Error: Something went wrong while setting rate limit for org: ${org}`, error);
cliux.error(`Error: Something went wrong while setting rate limit for org: ${org}`);
cliux.error(error);
}
}
}
1 change: 1 addition & 0 deletions packages/contentstack-config/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export interface SetRateLimitConfig {
utilize?: number;
limitName?: string[];
default?: boolean;
host?: string;
}
22 changes: 14 additions & 8 deletions packages/contentstack-config/src/utils/rate-limit-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ export class RateLimitHandler {
const features = organizations.plan?.features || [];

const limitsToUpdate = { ...rateLimit[config.org] };

limitNames.forEach((limitName, index) => {
if (limitNamesConfig.includes(limitName)) {
const feature = features.find((f: { uid: string }) => f.uid === limitName);
if (feature) {
let index = 0;
limitNamesConfig.forEach((limitName) => {
const feature = features.find((f: { uid: string }) => f.uid === limitName);
if (feature) {
if (limitNames.includes(limitName)) {
limitsToUpdate[limitName] = {
value: feature.limit || rateLimit[config.org][limitName]?.value || rateLimit.default[limitName]?.value,
utilize: utilizeValues[index] || defaultRalteLimitConfig[limitName]?.utilize,
};
index++;
} else {
limitsToUpdate[limitName] = {
value: rateLimit[config.org][limitName]?.value || rateLimit.default[limitName]?.value,
utilize: utilizeValues[index] || Number(config.utilize[0]),
value: feature.limit,
utilize: defaultRalteLimitConfig[limitName]?.utilize,
};
}
}
Expand All @@ -47,7 +53,7 @@ export class RateLimitHandler {
configHandler.set('rateLimit', rateLimit);
cliux.success(`Rate limit has been set successfully for org: ${config.org}`);
} catch (error) {
cliux.error(`Error: Unable to set the rate limit`, error?.errorMessage || error?.message || error);
throw new Error(error);
}
}
}

0 comments on commit 4c73e13

Please sign in to comment.