placementt-core 11.0.803 → 11.0.914

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/reduxHooks.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { orderBy, where } from "firebase/firestore"
1
+ import { documentId, orderBy, where } from "firebase/firestore"
2
2
  import { useEffect, useState } from "react"
3
3
  import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"
4
4
  import { AppDispatch, RootState } from "./config"
@@ -8,8 +8,8 @@ import { editActivePlacement, fetchActivePlacement, setActivePlacement } from ".
8
8
  import { addCompletedStudentPlacements, deleteCompletedStudentPlacement, setCompletedStudentPlacements, updateCompletedStudentPlacement } from "./features/placements/studentPlacements/completedStudentPlacementsSlice"
9
9
  import { addUpcomingStudentPlacements, deleteUpcomingStudentPlacement, setUpcoming, setUpcomingStudentPlacements, updateUpcomingStudentPlacement } from "./features/placements/studentPlacements/upcomingStudentPlacementsSlice"
10
10
  import FirebaseQuery from "./firebase/firebaseQuery"
11
- import { Address, Application, Contact, PlacementListing, ProviderData, StudentPlacementData, UserData } from "./typeDefinitions"
12
- import {getPlacementsWhere} from "./firebase/readDatabase"
11
+ import { Address, Application, CohortData, Contact, EmailTemplate, InstituteData, PlacementListing, ProviderData, StudentPlacementData, UserData, UserGroupData } from "./typeDefinitions"
12
+ import {getOrganisation, getPlacementsWhere} from "./firebase/readDatabase"
13
13
  import {convertDate} from "./firebase/util"
14
14
 
15
15
  export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
@@ -17,7 +17,7 @@ export const useAppDispatch = () => useDispatch<AppDispatch>();
17
17
 
18
18
 
19
19
  export function useStudent({user} : {user: UserData}) {
20
- if (!user || user.product !== "students") return {};
20
+ if (!user || user.userType !== "Students") return {error: "Unauthorized"};
21
21
 
22
22
  const upcomingPlacements = useAppSelector((state) => state.upcomingStudentPlacements.values)
23
23
  const completedPlacements = useAppSelector((state) => state.completedStudentPlacements.values)
@@ -45,9 +45,7 @@ export function useStudent({user} : {user: UserData}) {
45
45
  const applicationsWithProviderAndListing:{[key: string]: Application&{listing: PlacementListing, provider: ProviderData, address: Address}} = Object.fromEntries(await Promise.all(Object.entries(fApplications).map(async ([id, application]) => {
46
46
  const provider = await firebaseQuery.getDocData(["providers", application.providerId]).catch(() => false) as ProviderData|false;
47
47
  const listing = await firebaseQuery.getDocData(["placementListings", application.listingId]).catch(() => false) as PlacementListing|false;
48
- const address = listing && listing?.addressId && await firebaseQuery.getDocData(["addresses", listing.addressId]) as Address;
49
-
50
- return [id, {...application, listing: listing, provider: provider, address: address}];
48
+ return [id, {...application, listing: listing, provider: provider}];
51
49
  })));
52
50
 
53
51
  setApplications(applicationsWithProviderAndListing);
@@ -162,10 +160,28 @@ export function useStudent({user} : {user: UserData}) {
162
160
 
163
161
 
164
162
  export function useInstituteStaff({user}: {user: UserData}) {
163
+ if (!user || user?.product !== "institutes" || user.userType !== "Staff") return {error: "Unauthorized"};
164
+
165
165
  const firebaseQuery = new FirebaseQuery();
166
166
  const dispatch = useAppDispatch();
167
+ const {userGroups, forms, institute} = useInstitute({user})
168
+ const [cohorts, setCohorts] = useState<{[key:string]: CohortData}>();
169
+ const [emailTemplates, setEmailTemplates] = useState<{[key:string]: EmailTemplate}>();
170
+
167
171
 
168
- if (!user || user?.product !== "institutes" || user.userType !== "Staff") return {};
172
+ useEffect(() => {
173
+ const cohortConstraints = [where("oId", "==", user.oId)];
174
+ if (user.userGroup !== "admin") {
175
+ if (user.viewAddresses !== "all") {
176
+ cohortConstraints.push(where("addressId", "in", user.visibleAddresses));
177
+ }
178
+ if (user.viewCohorts !== "all") {
179
+ cohortConstraints.push(where(documentId(), "in", user.visibleCohorts));
180
+ }
181
+ }
182
+ firebaseQuery.collectionSnapshot(setCohorts, "cohorts", cohortConstraints);
183
+ firebaseQuery.collectionSnapshot(setEmailTemplates, "emailTemplates", [where("product", "==", user.product), where("oId", "==", user.oId)]);
184
+ }, [user.oId, user.userGroup, user.viewAddresses, user.viewCohorts, user.visibleAddresses, user.visibleCohorts, user.product])
169
185
 
170
186
  const getJobById = async (id: string) => {
171
187
  try {
@@ -185,26 +201,73 @@ export function useInstituteStaff({user}: {user: UserData}) {
185
201
  setJobStatus: async ({jobId, status}: {jobId: string, status: string}) => dispatch(setJobStatus({jobId: jobId, status: status})),
186
202
  updateJob: async ({jobId, data}: {jobId: string, data: string}) => dispatch(updateJob({jobId: jobId, data: data})),
187
203
  getById: getJobById
188
- }
204
+ },
205
+ institute,
206
+ userGroups,
207
+ forms,
208
+ cohorts,
209
+ emailTemplates,
189
210
  }
211
+ }
212
+
213
+ export function useInstitute({user}: {user: UserData}) {
214
+ if (!user || user?.product !== "institutes") return {error: "Unauthorized"};
215
+
216
+ const [userGroups, setUserGroups] = useState<{[key:string]: UserGroupData}>();
217
+ const [forms, setForms] = useState<{[key:string]: unknown}>();
218
+ const [institute, setInstitute] = useState<InstituteData>();
219
+ const firebaseQuery = new FirebaseQuery();
190
220
 
191
221
 
222
+ useEffect(() => {
223
+ getOrganisation(user, (data) => setInstitute(data as InstituteData));
224
+ firebaseQuery.collectionSnapshot(setUserGroups, "userGroups", [where("oId", "==", user.oId), where("product", "==", user.product)]);
225
+ firebaseQuery.collectionSnapshot(setForms, "forms", [where("oId", "==", user.oId), where("product", "==", user.product)]);
226
+ }, [user.product, user.oId])
227
+
228
+ return {
229
+ userGroups,
230
+ forms,
231
+ institute,
232
+ }
233
+
192
234
  }
193
235
 
194
236
  export function useInstituteStudent({user}: {user: UserData}) {
195
- const {contacts, placements} = useStudent({user});
237
+ if (!user || user?.product !== "institutes" || user.userType !== "Students") return {error: "Unauthorized"};
196
238
 
197
- if (!user || user?.product !== "institutes" || user.userType !== "Students") return {};
239
+ const {contacts, placements, applications} = useStudent({user});
240
+ const [cohorts, setCohorts] = useState<{[key:string]: CohortData}>();
241
+ const firebaseQuery = new FirebaseQuery();
242
+ const {userGroups, forms, institute} = useInstitute({user})
243
+
244
+ useEffect(() => {
245
+ const isAdmin = user.userGroup === "admin";
246
+ const canViewCohorts = user.viewCohorts !== "none";
247
+ const canViewAddresses = user.viewAddresses !== "none";
248
+
249
+ if (isAdmin || (canViewCohorts && canViewAddresses)) {
250
+ const cohortConstraints = [where("oId", "==", user.oId)];
251
+ firebaseQuery.collectionSnapshot(setCohorts, "cohorts", cohortConstraints);
252
+ } else {
253
+ setCohorts({});
254
+ }
255
+ }, [user.oId, user.id, user.userGroup, user.viewCohorts, user.viewAddresses, user.product]);
198
256
 
199
257
  return {
200
258
  contacts,
201
- placements
259
+ placements,
260
+ institute,
261
+ userGroups,
262
+ forms,
263
+ cohorts,
264
+ applications,
202
265
  }
203
266
 
204
267
  }
205
268
 
206
269
  export function useProvider({user}: {user: UserData}) {
207
- if (!user || user?.product !== "providers") return {};
270
+ if (!user || user?.product !== "providers") return {error: "Unauthorized"};
208
271
 
209
272
  const {jobs} = useInstituteStaff({user});
210
273