Skip to content

Commit ac3a8ce

Browse files
committed
central config and allow accessing events api prod from website staging
1 parent df7f133 commit ac3a8ce

File tree

5 files changed

+60
-52
lines changed

5 files changed

+60
-52
lines changed

src/config.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type EnvironmentConfigType = {
2323
const genericConfig: GenericConfigType = {
2424
DynamoTableName: "infra-events-api-records",
2525
ConfigSecretName: "infra-events-api-config",
26-
UpcomingEventThresholdSeconds: 1800 // 30 mins
26+
UpcomingEventThresholdSeconds: 1800, // 30 mins
2727
} as const;
2828

2929
const environmentConfig: EnvironmentConfigType = {
@@ -35,8 +35,11 @@ const environmentConfig: EnvironmentConfigType = {
3535
"0": [AppRoles.MANAGER], // Dummy Group for development only
3636
},
3737
AzureRoleMapping: { AutonomousWriters: [AppRoles.MANAGER] },
38-
ValidCorsOrigins: ['http://localhost:3000', /\.acmuiuc\.\.pages\.dev$/, 'https://acmuiuc.pages.dev'],
39-
AadValidClientId: '39c28870-94e4-47ee-b4fb-affe0bf96c9f'
38+
ValidCorsOrigins: [
39+
"http://localhost:3000",
40+
/\.acmuiuc\.\.pages\.dev$/,
41+
],
42+
AadValidClientId: "39c28870-94e4-47ee-b4fb-affe0bf96c9f",
4043
},
4144
prod: {
4245
GroupRoleMapping: {
@@ -45,8 +48,12 @@ const environmentConfig: EnvironmentConfigType = {
4548
"ad81254b-4eeb-4c96-8191-3acdce9194b1": [AppRoles.MANAGER], // Exec
4649
},
4750
AzureRoleMapping: { AutonomousWriters: [AppRoles.MANAGER] },
48-
ValidCorsOrigins: ['https:///acm.illinois.edu', 'https:///www.acm.illinois.edu'],
49-
AadValidClientId: '5e08cf0f-53bb-4e09-9df2-e9bdc3467296'
51+
ValidCorsOrigins: [
52+
"https:///acm.illinois.edu",
53+
"https:///www.acm.illinois.edu",
54+
/\.acmuiuc\.\.pages\.dev$/,
55+
],
56+
AadValidClientId: "5e08cf0f-53bb-4e09-9df2-e9bdc3467296",
5057
},
5158
} as const;
5259

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async function init() {
4141
});
4242
}
4343
app.runEnvironment = process.env.RunEnvironment as RunEnvironment;
44+
app.environmentConfig = environmentConfig[app.runEnvironment];
4445
app.addHook("onRequest", (req, _, done) => {
4546
req.startTime = now();
4647
req.log.info({ url: req.raw.url }, "received request");

src/plugins/auth.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
UnauthenticatedError,
1414
UnauthorizedError,
1515
} from "../errors/index.js";
16-
import { environmentConfig, genericConfig } from "../config.js";
16+
import { genericConfig } from "../config.js";
1717

1818
function intersection<T>(setA: Set<T>, setB: Set<T>): Set<T> {
1919
const _intersection = new Set<T>();
@@ -159,35 +159,31 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
159159
request.username = verifiedTokenData.email || verifiedTokenData.sub;
160160
const userRoles = new Set([] as AppRoles[]);
161161
const expectedRoles = new Set(validRoles);
162-
if (verifiedTokenData.groups) {
162+
if (
163+
verifiedTokenData.groups &&
164+
fastify.environmentConfig.GroupRoleMapping
165+
) {
163166
for (const group of verifiedTokenData.groups) {
164-
if (
165-
!environmentConfig[fastify.runEnvironment]["GroupRoleMapping"][
167+
if (fastify.environmentConfig["GroupRoleMapping"][group]) {
168+
for (const role of fastify.environmentConfig["GroupRoleMapping"][
166169
group
167-
]
168-
) {
169-
continue;
170-
}
171-
for (const role of environmentConfig[fastify.runEnvironment][
172-
"GroupRoleMapping"
173-
][group]) {
174-
userRoles.add(role);
170+
]) {
171+
userRoles.add(role);
172+
}
175173
}
176174
}
177175
} else {
178-
if (verifiedTokenData.roles) {
176+
if (
177+
verifiedTokenData.roles &&
178+
fastify.environmentConfig.AzureRoleMapping
179+
) {
179180
for (const group of verifiedTokenData.roles) {
180-
if (
181-
!environmentConfig[fastify.runEnvironment]["AzureRoleMapping"][
182-
group
183-
]
184-
) {
185-
continue;
186-
}
187-
for (const role of environmentConfig[fastify.runEnvironment][
188-
"AzureRoleMapping"
189-
][group]) {
190-
userRoles.add(role);
181+
if (fastify.environmentConfig["AzureRoleMapping"][group]) {
182+
for (const role of fastify.environmentConfig[
183+
"AzureRoleMapping"
184+
][group]) {
185+
userRoles.add(role);
186+
}
191187
}
192188
}
193189
} else {

src/routes/events.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { genericConfig } from "../config.js";
1212
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
1313
import { DatabaseFetchError, DatabaseInsertError } from "../errors/index.js";
1414
import { randomUUID } from "crypto";
15-
import moment from 'moment-timezone';
15+
import moment from "moment-timezone";
1616

1717
// POST
1818

@@ -38,10 +38,8 @@ const requestBodySchema = baseBodySchema
3838
message: "repeats is required when repeatEnds is defined",
3939
});
4040

41-
4241
type EventPostRequest = z.infer<typeof requestBodySchema>;
4342

44-
4543
const responseJsonSchema = zodToJsonSchema(
4644
z.object({
4745
id: z.string(),
@@ -52,14 +50,7 @@ const responseJsonSchema = zodToJsonSchema(
5250
// GET
5351
const getResponseBodySchema = z.array(requestBodySchema);
5452
const getResponseJsonSchema = zodToJsonSchema(getResponseBodySchema);
55-
56-
57-
const getQueryParams = z.object({
58-
upcomingOnly: z.boolean().default(false),
59-
})
60-
type EventsGetQueryParams = z.infer<typeof getQueryParams>;
61-
const getQueryParamsJsonSchema = zodToJsonSchema(getResponseBodySchema);
62-
53+
type EventsGetQueryParams = { upcomingOnly?: boolean };
6354

6455
const dynamoClient = new DynamoDBClient({
6556
region: process.env.AWS_REGION || "us-east-1",
@@ -108,13 +99,16 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
10899
}
109100
},
110101
);
111-
type EventsGetRequest = {Body: undefined, Querystring?: EventsGetQueryParams}
112-
fastify.get<EventsGetRequest>(
102+
type EventsGetRequest = {
103+
Body: undefined;
104+
Querystring?: EventsGetQueryParams;
105+
};
106+
fastify.get<EventsGetRequest>(
113107
"/",
114108
{
115109
schema: {
116110
querystring: {
117-
upcomingOnly: { type: 'boolean' },
111+
upcomingOnly: { type: "boolean" },
118112
},
119113
response: { 200: getResponseJsonSchema },
120114
},
@@ -129,25 +123,34 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
129123
const currentTimeChicago = moment().tz("America/Chicago");
130124
let parsedItems = getResponseBodySchema.parse(items);
131125
if (upcomingOnly) {
132-
parsedItems = parsedItems.filter(item => {
126+
parsedItems = parsedItems.filter((item) => {
133127
try {
134-
if (!item.repeatEnds || item.repeatEnds === 'never') {
128+
if (!item.repeatEnds || item.repeatEnds === "never") {
135129
return true;
136130
}
137131
if (!item.repeats) {
138132
const end = item.end || item.start;
139-
const momentEnds = moment.tz(end, "America/Chicago")
133+
const momentEnds = moment.tz(end, "America/Chicago");
140134
const diffTime = currentTimeChicago.diff(momentEnds);
141-
return Boolean(diffTime <= genericConfig.UpcomingEventThresholdSeconds)
135+
return Boolean(
136+
diffTime <= genericConfig.UpcomingEventThresholdSeconds,
137+
);
142138
}
143-
const momentRepeatEnds = moment.tz(item.repeatEnds, "America/Chicago")
139+
const momentRepeatEnds = moment.tz(
140+
item.repeatEnds,
141+
"America/Chicago",
142+
);
144143
const diffTime = currentTimeChicago.diff(momentRepeatEnds);
145-
return Boolean(diffTime <= genericConfig.UpcomingEventThresholdSeconds);
146-
} catch (e) {
147-
request.log.warn(`Could not compute upcoming event status for event ${item.title}!`)
144+
return Boolean(
145+
diffTime <= genericConfig.UpcomingEventThresholdSeconds,
146+
);
147+
} catch (e: unknown) {
148+
request.log.warn(
149+
`Could not compute upcoming event status for event ${item.title}: ${e instanceof Error ? e.toString() : e}`,
150+
);
148151
return false;
149152
}
150-
})
153+
});
151154
}
152155
reply.send(parsedItems);
153156
} catch (e: unknown) {

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare module "fastify" {
1818
zodSchema: Zod.ZodTypeAny,
1919
) => Promise<void>;
2020
runEnvironment: RunEnvironment;
21+
environmentConfig: ConfigType;
2122
}
2223
interface FastifyRequest {
2324
startTime: number;

0 commit comments

Comments
 (0)