Skip to content

Commit e18177e

Browse files
committed
siglead main page api working (fetching from dynamo db only)
* pulls sigid and counts, displays on main page * routing to detail page working * need to implement tarash's name function * tests not written yet
1 parent 11f2d98 commit e18177e

File tree

5 files changed

+135
-26
lines changed

5 files changed

+135
-26
lines changed

src/api/functions/siglead.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
ScanCommand,
55
} from "@aws-sdk/client-dynamodb";
66
import { unmarshall } from "@aws-sdk/util-dynamodb";
7-
import { SigDetailRecord, SigMemberRecord } from "common/types/siglead.js";
8-
import { FastifyRequest } from "fastify";
7+
import {
8+
SigDetailRecord,
9+
SigMemberCount,
10+
SigMemberRecord,
11+
} from "common/types/siglead.js";
912

1013
export async function fetchMemberRecords(
1114
sigid: string,
@@ -63,3 +66,36 @@ export async function fetchSigDetail(
6366
return unmarshalledItem as SigDetailRecord;
6467
})[0];
6568
}
69+
70+
// select count(sigid)
71+
// from table
72+
// groupby sigid
73+
export async function fetchSigCounts(
74+
sigMemberTableName: string,
75+
dynamoClient: DynamoDBClient,
76+
) {
77+
const scan = new ScanCommand({
78+
TableName: sigMemberTableName,
79+
ProjectionExpression: "sigGroupId",
80+
});
81+
82+
const result = await dynamoClient.send(scan);
83+
84+
const counts: Record<string, number> = {};
85+
86+
(result.Items || []).forEach((item) => {
87+
const sigGroupId = item.sigGroupId?.S;
88+
if (sigGroupId) {
89+
counts[sigGroupId] = (counts[sigGroupId] || 0) + 1;
90+
}
91+
});
92+
93+
const countsArray: SigMemberCount[] = Object.entries(counts).map(
94+
([sigid, count]) => ({
95+
sigid,
96+
count,
97+
}),
98+
);
99+
console.log(countsArray);
100+
return countsArray;
101+
}

src/api/routes/siglead.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ import { zodToJsonSchema } from "zod-to-json-schema";
4040
import {
4141
SigDetailRecord,
4242
SigleadGetRequest,
43+
SigMemberCount,
4344
SigMemberRecord,
4445
} from "common/types/siglead.js";
45-
import { fetchMemberRecords, fetchSigDetail } from "api/functions/siglead.js";
46+
import {
47+
fetchMemberRecords,
48+
fetchSigCounts,
49+
fetchSigDetail,
50+
} from "api/functions/siglead.js";
4651
import { intersection } from "api/plugins/auth.js";
4752

4853
const sigleadRoutes: FastifyPluginAsync = async (fastify, _options) => {
@@ -124,7 +129,42 @@ const sigleadRoutes: FastifyPluginAsync = async (fastify, _options) => {
124129
reply.code(200).send(sigDetail);
125130
},
126131
);
132+
133+
// fetch sig count
134+
fastify.get<SigleadGetRequest>(
135+
"/sigcount",
136+
{
137+
onRequest: async (request, reply) => {
138+
/*await fastify.authorize(request, reply, [
139+
AppRoles.LINKS_MANAGER,
140+
AppRoles.LINKS_ADMIN,
141+
]);*/
142+
},
143+
},
144+
async (request, reply) => {
145+
// First try-catch: Fetch owner records
146+
let sigMemCounts: SigMemberCount[];
147+
try {
148+
sigMemCounts = await fetchSigCounts(
149+
genericConfig.SigleadDynamoSigMemberTableName,
150+
fastify.dynamoClient,
151+
);
152+
} catch (error) {
153+
request.log.error(
154+
`Failed to fetch sig member counts record: ${error instanceof Error ? error.toString() : "Unknown error"}`,
155+
);
156+
throw new DatabaseFetchError({
157+
message:
158+
"Failed to fetch sig member counts record from Dynamo table.",
159+
});
160+
}
161+
162+
// Send the response
163+
reply.code(200).send(sigMemCounts);
164+
},
165+
);
127166
};
167+
128168
fastify.register(limitedRoutes);
129169
};
130170

src/common/types/siglead.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
export type SigDetailRecord = {
2-
sigid: string;
3-
signame: string;
4-
description: string;
5-
};
6-
7-
export type SigMemberRecord = {
8-
sigGroupId: string;
9-
email: string;
10-
designation: string;
11-
memberName: string;
12-
};
13-
14-
export type SigleadGetRequest = {
15-
Params: { sigid: string };
16-
Querystring: undefined;
17-
Body: undefined;
18-
};
2+
sigid: string;
3+
signame: string;
4+
description: string;
5+
};
6+
7+
export type SigMemberRecord = {
8+
sigGroupId: string;
9+
email: string;
10+
designation: string;
11+
memberName: string;
12+
};
13+
14+
export type SigleadGetRequest = {
15+
Params: { sigid: string };
16+
Querystring: undefined;
17+
Body: undefined;
18+
};
19+
20+
export type SigMemberCount = {
21+
sigid: string;
22+
count: number;
23+
};

src/ui/pages/siglead/ManageSigLeads.page.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ScreenComponent } from './SigScreenComponents';
2525
import { GroupMemberGetResponse } from '@common/types/iam';
2626
import { transformCommaSeperatedName } from '@common/utils';
2727
import { orgsGroupId } from '@common/config';
28+
import { SigMemberCount } from '@common/types/siglead';
2829

2930
export function capitalizeFirstLetter(string: string) {
3031
return string.charAt(0).toUpperCase() + string.slice(1);
@@ -65,6 +66,7 @@ type EventPostRequest = z.infer<typeof requestBodySchema>;
6566

6667
export const ManageSigLeadsPage: React.FC = () => {
6768
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
69+
const [SigMemberCounts, setSigMemberCounts] = useState<SigMemberCount[]>([]);
6870
const navigate = useNavigate();
6971
const api = useApi('core');
7072

@@ -105,6 +107,25 @@ export const ManageSigLeadsPage: React.FC = () => {
105107
getEvent();
106108
}, [eventId, isEditing]);
107109

110+
useEffect(() => {
111+
const getMemberCounts = async () => {
112+
try {
113+
console.log('fetching counts');
114+
/*const formValues = {
115+
};
116+
form.setValues(formValues);*/
117+
const sigMemberCountsRequest = await api.get(`/api/v1/siglead/sigcount`);
118+
setSigMemberCounts(sigMemberCountsRequest.data);
119+
} catch (error) {
120+
console.error('Error fetching sig member counts:', error);
121+
notifications.show({
122+
message: 'Failed to fetch sig member counts, please try again.',
123+
});
124+
}
125+
};
126+
getMemberCounts();
127+
}, []); // empty dependency array to only run once
128+
108129
const form = useForm<EventPostRequest>({
109130
validate: zodResolver(requestBodySchema),
110131
initialValues: {
@@ -204,7 +225,7 @@ export const ManageSigLeadsPage: React.FC = () => {
204225
<Container>
205226
<TestButton />
206227
<Title order={2}>SigLead Management System</Title>
207-
<ScreenComponent />
228+
<ScreenComponent SigMemberCounts={SigMemberCounts} />
208229
{/* <SigTable /> */}
209230
</Container>
210231
</AuthGuard>

src/ui/pages/siglead/SigScreenComponents.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import { OrganizationList } from '@common/orgs';
33
import { NavLink, Paper } from '@mantine/core';
44
import { IconUsersGroup } from '@tabler/icons-react';
55
import { useLocation } from 'react-router-dom';
6+
import { SigMemberCount } from '@common/types/siglead';
67

7-
const renderSigLink = (org: string, index: number) => {
8+
const renderSigLink = (sigMemCount: SigMemberCount, index: number) => {
89
const color = 'light-dark(var(--mantine-color-black), var(--mantine-color-white))';
910
const size = '18px';
11+
const org = sigMemCount.sigid;
12+
const count = sigMemCount.count;
1013
return (
1114
<NavLink
12-
href={`${useLocation().pathname}/${org}`}
15+
href={`${useLocation().pathname}${org}`}
1316
active={index % 2 === 0}
1417
label={org}
1518
color="var(--mantine-color-blue-light)"
@@ -24,7 +27,7 @@ const renderSigLink = (org: string, index: number) => {
2427
fontSize: `${size}`,
2528
}}
2629
>
27-
<span>MemberCount[{index}]</span>
30+
<span>{count}</span>
2831
<IconUsersGroup />
2932
</div>
3033
}
@@ -38,7 +41,10 @@ const renderSigLink = (org: string, index: number) => {
3841
);
3942
};
4043

41-
export const ScreenComponent: React.FC = () => {
44+
type props = {
45+
SigMemberCounts: SigMemberCount[];
46+
};
47+
export const ScreenComponent: React.FC<props> = ({ SigMemberCounts }) => {
4248
return (
4349
<>
4450
<Paper
@@ -58,7 +64,8 @@ export const ScreenComponent: React.FC = () => {
5864
<span>Organization</span>
5965
<span>Member Count</span>
6066
</Paper>
61-
{OrganizationList.map(renderSigLink)}
67+
{/* {OrganizationList.map(renderSigLink)} */}
68+
{SigMemberCounts.map(renderSigLink)}
6269
</>
6370
);
6471
};

0 commit comments

Comments
 (0)