Skip to content

Commit

Permalink
feat: allow limiting max orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 7, 2020
1 parent 6652a43 commit e9720e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/entities/orgs/guards/max-org-guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { guard } from '../../../commons/express/guard';
import { Orgs } from '../org';
import { env } from '../../../env';

export const maxOrgsGuard = guard(
async req => {
if (env.MELI_MAX_ORGS === 0) {
return true;
}
const count = await Orgs().countDocuments({}, { limit: 1 });
return count < env.MELI_MAX_ORGS;
},
'Cannot create more orgs',
);
2 changes: 2 additions & 0 deletions src/entities/orgs/handlers/create-org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { object, string } from 'joi';
import { uuid } from '../../../utils/uuid';
import { serializeUserOrg } from '../serialize-user-org';
import { EventType } from '../../../events/app-event';
import { maxOrgsGuard } from '../guards/max-org-guard';

const validators = [
body(object({
Expand Down Expand Up @@ -77,5 +78,6 @@ async function handler(req: Request, res: Response): Promise<void> {

export const createOrg = [
...validators,
maxOrgsGuard,
wrapAsyncMiddleware(handler),
];
13 changes: 7 additions & 6 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import chalk from 'chalk';
import { cidrSubnet } from 'ip';
import {
array, boolean, number, string,
} from 'joi';
import { array, boolean, number, string } from 'joi';
import { tmpdir } from 'os';
import { EnvSpec, parseEnv } from './commons/env/parse-env';
import {
commaSeparatedStringToArray, stringToBoolean, stringToInt,
} from './commons/env/transformers';
import { commaSeparatedStringToArray, stringToBoolean, stringToInt } from './commons/env/transformers';
import { AppError } from './commons/errors/app-error';
import { isUrl } from './commons/validators/is-url';
import { Logger } from './commons/logger/logger';
Expand Down Expand Up @@ -75,6 +71,7 @@ export interface Env {
MELI_AXIOS_TIMEOUT: number;
MELI_USER: string;
MELI_PASSWORD: string;
MELI_MAX_ORGS: number;
}

const envSpec: EnvSpec<Env> = {
Expand Down Expand Up @@ -311,6 +308,10 @@ const envSpec: EnvSpec<Env> = {
MELI_PASSWORD: {
schema: string().optional(),
},
MELI_MAX_ORGS: {
transform: stringToInt(),
schema: number().optional().default(1),
},
};

export const env: Env = parseEnv(envSpec);
Expand Down

0 comments on commit e9720e9

Please sign in to comment.