Skip to content

Commit 4365b64

Browse files
anantmittalnatalierobbinsCopilot
authored
refactor: CASL, Timezone, Database Scripts (#149)
* fix: Client CASL (#138) * fix: client casl * simplyify survey fetch * simplify fetch and normalize function * review: add comments for Natalie * deserialization updates * timezone handling updates * refactor: consolidate useApi * refactor: update signup page with api response structure * refactor: mc --------- Co-authored-by: Anant Mittal <anmittal@uw.edu> * feat: location script (#137) * feat: location script * bulk import options * refactor: moved location CRUD script to server/src - resolves absolute import statements - IDE recognizes node packages * remove: old location crud file --------- Co-authored-by: Anant Mittal <anmittal@uw.edu> * Timezone Fixes (#153) * fix: client-side permission checks will always use Pacific timezone regardless of the server TIMEZONE configuration, potentially causing inconsistencies between client and server permission evaluations * Update client/src/hooks/useAbility.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update server/.env.example Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update client/src/components/forms/LocationSelect.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * revert: gh copilot fix * refactor: move all scripts to server folder --------- Co-authored-by: Natalie Robbins <102693174+natalierobbins@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3bbb96c commit 4365b64

File tree

34 files changed

+2017
-762
lines changed

34 files changed

+2017
-762
lines changed

client/src/components/forms/LocationSelect.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ export const LocationSelect = ({
1616
...props
1717
}: LocationSelectProps) => {
1818
const { locationService } = useApi();
19-
const { data: locations = [] } = locationService.useLocations() || {};
19+
const { data: locations = [] } = locationService.useLocations() ?? {};
2020

21-
const options = locations.map(
21+
const options = locations?.map(
2222
(location: { _id: string; hubName: string }) => ({
2323
value: location._id,
2424
label: location.hubName
2525
})
2626
);
2727

2828
const optionsWithEmpty = includeEmptyOption
29-
? [{ value: '', label: 'Select a location' }, ...options]
29+
? [{ value: '', label: 'Select a location' }, ...(options ?? [])]
3030
: options;
3131

3232
return (
3333
<FormSelect
3434
{...props}
3535
label="Location"
36-
options={optionsWithEmpty}
36+
options={optionsWithEmpty ?? []}
3737
canEdit={canEdit}
3838
showTooltip={showTooltip}
3939
/>

client/src/contexts/AuthContext.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface AuthState {
2828
subject: Subject;
2929
conditions: Condition[];
3030
}[];
31+
serverTimezone: string;
3132
isLoggedIn: boolean;
3233
isLoading: boolean;
3334
}
@@ -49,12 +50,16 @@ interface AuthContextValue extends AuthState {
4950
conditions: Condition[];
5051
}[]
5152
) => void;
53+
setServerTimezone: (serverTimezone: string) => void;
5254
clearSession: () => void;
5355
fetchUserContext: (userObjectId: string) => Promise<void>;
5456
}
5557

5658
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
5759

60+
// Default timezone fallback for client-side (matches server default)
61+
const DEFAULT_CLIENT_TIMEZONE = 'America/Los_Angeles';
62+
5863
function getDefaultAuthState(): AuthState {
5964
return {
6065
token: '',
@@ -67,6 +72,7 @@ function getDefaultAuthState(): AuthState {
6772
locationObjectId: '',
6873
lastestLocationObjectId: '',
6974
permissions: [],
75+
serverTimezone: DEFAULT_CLIENT_TIMEZONE,
7076
isLoggedIn: false,
7177
isLoading: false
7278
};
@@ -91,6 +97,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
9197
permissions: [],
9298
locationObjectId: '',
9399
lastestLocationObjectId: '',
100+
serverTimezone: DEFAULT_CLIENT_TIMEZONE,
94101
isLoggedIn: true,
95102
isLoading: true
96103
};
@@ -151,6 +158,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
151158
locationObjectId: user.data.locationObjectId,
152159
lastestLocationObjectId: latestLocationObjectId,
153160
permissions: user.data.permissions,
161+
serverTimezone: user.serverTimezone ?? DEFAULT_CLIENT_TIMEZONE,
154162
isLoggedIn: true,
155163
isLoading: false
156164
}));
@@ -200,6 +208,10 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
200208
setState(prev => ({ ...prev, permissions }));
201209
};
202210

211+
const setServerTimezone = (serverTimezone: string) => {
212+
setState(prev => ({ ...prev, serverTimezone }));
213+
};
214+
203215
const clearSession = () => {
204216
deleteAuthToken(); // Clears from Zustand
205217
setState(getDefaultAuthState());
@@ -218,6 +230,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
218230
setLocationObjectId,
219231
setLastestLocationObjectId,
220232
setPermissions,
233+
setServerTimezone,
221234
clearSession,
222235
fetchUserContext
223236
};

client/src/hooks/useAbility.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { useAuthContext } from '@/contexts';
44
import defineAbilitiesForUser from '@/permissions/abilityBuilder';
55

66
export const useAbility = () => {
7-
const { userRole, userObjectId, lastestLocationObjectId, permissions } =
8-
useAuthContext();
7+
const {
8+
userRole,
9+
userObjectId,
10+
lastestLocationObjectId,
11+
permissions,
12+
serverTimezone
13+
} = useAuthContext();
914

1015
// updates whenever our auth store updates
1116
return useMemo(
@@ -14,8 +19,15 @@ export const useAbility = () => {
1419
userRole ?? '',
1520
userObjectId ?? '',
1621
lastestLocationObjectId ?? '',
17-
permissions
22+
permissions,
23+
serverTimezone ?? 'America/Los_Angeles'
1824
),
19-
[userRole, userObjectId, lastestLocationObjectId, permissions]
25+
[
26+
userRole,
27+
userObjectId,
28+
lastestLocationObjectId,
29+
permissions,
30+
serverTimezone
31+
]
2032
);
2133
};

0 commit comments

Comments
 (0)