Refactor to accommodate authentication, transaction and pagination states together (#4636)

* fix: refactor to accommodate authentication, transaction and pagination together into ctx.state

* feat: allow passing response type to APIContext
This commit is contained in:
Apoorv Mishra
2023-01-04 23:51:44 +05:30
committed by GitHub
parent bb568d2e62
commit f4461573de
31 changed files with 753 additions and 675 deletions

View File

@@ -6,6 +6,7 @@ import env from "@server/env";
import Logger from "@server/logging/Logger";
import auth from "@server/middlewares/authentication";
import { presentUser } from "@server/presenters";
import { APIContext } from "@server/types";
const router = new Router();
@@ -19,40 +20,45 @@ function dev() {
};
}
router.post("developer.create_test_users", dev(), auth(), async (ctx) => {
const { count = 10 } = ctx.request.body;
const { user } = ctx.state;
const invites = Array(Math.min(count, 100))
.fill(0)
.map(() => {
const rando = randomstring.generate(10);
router.post(
"developer.create_test_users",
dev(),
auth(),
async (ctx: APIContext) => {
const { count = 10 } = ctx.request.body;
const { user } = ctx.state.auth;
const invites = Array(Math.min(count, 100))
.fill(0)
.map(() => {
const rando = randomstring.generate(10);
return {
email: `${rando}@example.com`,
name: `${rando.slice(0, 5)} Tester`,
role: "member",
} as Invite;
return {
email: `${rando}@example.com`,
name: `${rando.slice(0, 5)} Tester`,
role: "member",
} as Invite;
});
Logger.info("utils", `Creating ${count} test users`, invites);
// Generate a bunch of invites
const response = await userInviter({
user,
invites,
ip: ctx.request.ip,
});
Logger.info("utils", `Creating ${count} test users`, invites);
// Convert from invites to active users by marking as active
await Promise.all(
response.users.map((user) => user.updateActiveAt(ctx, true))
);
// Generate a bunch of invites
const response = await userInviter({
user,
invites,
ip: ctx.request.ip,
});
// Convert from invites to active users by marking as active
await Promise.all(
response.users.map((user) => user.updateActiveAt(ctx, true))
);
ctx.body = {
data: {
users: response.users.map((user) => presentUser(user)),
},
};
});
ctx.body = {
data: {
users: response.users.map((user) => presentUser(user)),
},
};
}
);
export default router;