Skip to content

Commit c1debcf

Browse files
authored
Force syncing on admin sync page (#266)
Don't run isRequired check on sync page - just force the sync.
1 parent 5151137 commit c1debcf

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/app/(membership)/admin/sync/page.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import axios from 'axios';
2121
import Layout from '../../MembershipLayout';
2222
import successAnimation from '../../success.json';
2323
import { useSearchParams } from 'next/navigation';
24-
import { type IPublicClientApplication } from "@azure/msal-browser";
24+
import { type IPublicClientApplication } from '@azure/msal-browser';
2525
import { getUserAccessToken, initMsalClient } from '@/utils/msal';
2626
import { syncIdentity } from '@/utils/api';
2727

28-
2928
interface ErrorCode {
3029
code?: number | string;
3130
message: string;
@@ -52,14 +51,14 @@ const Sync = () => {
5251
(async () => {
5352
setPca(await initMsalClient());
5453
})();
55-
}, [])
54+
}, []);
5655

5756
const syncHandler = useCallback(async () => {
5857
setIsLoading(true);
5958
if (!pca) {
6059
setErrorMessage({
6160
code: -1,
62-
message: "Failed to authenticate NetID."
61+
message: 'Failed to authenticate NetID.',
6362
});
6463
modalErrorMessage.onOpen();
6564
return;
@@ -68,13 +67,13 @@ const Sync = () => {
6867
if (!accessToken) {
6968
setErrorMessage({
7069
code: -1,
71-
message: "Failed to authenticate NetID."
70+
message: 'Failed to authenticate NetID.',
7271
});
7372
modalErrorMessage.onOpen();
7473
return;
7574
}
7675

77-
syncIdentity(accessToken)
76+
syncIdentity(accessToken, true)
7877
.then(() => {
7978
modalSynced.onOpen();
8079
setIsLoading(false);
@@ -84,7 +83,7 @@ const Sync = () => {
8483
if (error.response) {
8584
setErrorMessage({
8685
code: error.response.status,
87-
message: "Failed to sync identity.",
86+
message: 'Failed to sync identity.',
8887
});
8988
}
9089
});
@@ -106,7 +105,8 @@ const Sync = () => {
106105
<Divider />
107106
<CardBody className="gap-4">
108107
<p>
109-
Only use this tool if prompted to by the ACM @ UIUC Infrastructure Team.
108+
Only use this tool if prompted to by the ACM @ UIUC Infrastructure
109+
Team.
110110
</p>
111111
<Button
112112
color="primary"
@@ -160,9 +160,7 @@ const Sync = () => {
160160
<ModalContent>
161161
<ModalHeader />
162162
<ModalBody className="flex flex-col items-center">
163-
<p className="text-center text-2xl font-bold">
164-
Identity synced!
165-
</p>
163+
<p className="text-center text-2xl font-bold">Identity synced!</p>
166164
<Lottie
167165
animationData={successAnimation}
168166
loop={false}

src/utils/api.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,44 @@ const baseUrl = process.env.NEXT_PUBLIC_EVENTS_API_BASE_URL || '';
66

77
export async function fetchUpcomingEvents() {
88
try {
9-
const response = await fetch(`${baseUrl}/api/v1/events?upcomingOnly=true&featuredOnly=true&includeMetadata=true`);
9+
const response = await fetch(
10+
`${baseUrl}/api/v1/events?upcomingOnly=true&featuredOnly=true&includeMetadata=true`,
11+
);
1012
const rawDates = (await response.json()) as IEvent[];
1113
return transformApiDates(rawDates);
1214
} catch (err: any) {
1315
return [];
1416
}
1517
}
1618

17-
export const syncIdentity = async (accessToken: string) => {
19+
export const syncIdentity = async (
20+
accessToken: string,
21+
force: boolean = false,
22+
) => {
1823
// If this fails we don't care its just best effort.
19-
const syncRequired = await checkIfSyncNeeded(accessToken)
20-
if (!syncRequired) {
21-
return;
24+
if (!force) {
25+
const syncRequired = await checkIfSyncNeeded(accessToken);
26+
if (!syncRequired) {
27+
return;
28+
}
2229
}
2330
const url = `${baseUrl}/api/v1/syncIdentity`;
2431
return await axios
25-
.post(url, {}, { headers: { "x-uiuc-token": accessToken } })
32+
.post(url, {}, { headers: { 'x-uiuc-token': accessToken } })
2633
.then(() => {
27-
console.log("Synced user identity")
34+
console.log('Synced user identity');
2835
})
2936
.catch((error) => {
30-
console.error(`Failed to sync user identity: ${error}`)
31-
})
37+
console.error(`Failed to sync user identity: ${error}`);
38+
});
3239
};
3340

34-
35-
export const checkIfSyncNeeded = async (accessToken: string): Promise<boolean> => {
41+
export const checkIfSyncNeeded = async (
42+
accessToken: string,
43+
): Promise<boolean> => {
3644
const url = `${baseUrl}/api/v1/syncIdentity/isRequired`;
3745
const response = await axios.get(url, {
38-
headers: { "x-uiuc-token": accessToken }
46+
headers: { 'x-uiuc-token': accessToken },
3947
});
4048
return response.data.syncRequired ?? false;
4149
};

0 commit comments

Comments
 (0)