-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge pull request #273 from tom-theret/service/uphf
feat(service): Ajout de l'Université Polytechnique Haut-de-France
- Loading branch information
Showing
15 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { UphfAccount } from "@/stores/account/types"; | ||
import { Information } from "../../shared/Information"; | ||
import { type ActualitiesResponse, UPHF } from "uphf-api"; | ||
import { AttachmentType } from "@/services/shared/Attachment"; | ||
|
||
const parseInformation = (i: ActualitiesResponse): Information => ({ | ||
id: i.pubDate, | ||
title: i.title, | ||
date: new Date(i.pubDate), | ||
acknowledged: false, | ||
attachments: [{"name": i.title,"type":"link" as AttachmentType, "url": i.link}], | ||
content: i.content, | ||
author: "UPHF Actualités", | ||
category: "Actualités", | ||
read: false, | ||
ref: i, | ||
}); | ||
|
||
export const getNews = async (account: UphfAccount): Promise<Information[]> => { | ||
const news = await UPHF.getActualities(); | ||
return news.map(parseInformation); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { UphfAccount } from "@/stores/account/types"; | ||
import type { Timetable, TimetableClass} from "../../shared/Timetable"; | ||
import { weekNumberToDateRange } from "@/utils/epochWeekNumber"; | ||
import type { EventResponse } from "uphf-api"; | ||
import { ErrorServiceUnauthenticated } from "@/services/shared/errors"; | ||
|
||
const decodeTimetableClass = (c: EventResponse): TimetableClass => ({ | ||
subject: c.course.label, | ||
id: c.id, | ||
type: "lesson", | ||
title: c.course.label, | ||
startTimestamp: new Date(c.startDateTime).getTime(), | ||
endTimestamp: new Date(c.endDateTime).getTime(), | ||
room: c.course.online ? "En ligne" : c.rooms.map((room: any) => room.label).join(", "), | ||
teacher: c.teachers.map((teacher: any) => teacher.displayname).join(", "), | ||
backgroundColor: c.course.color, | ||
source: "UPHF", | ||
}); | ||
|
||
export const getTimetableForWeek = async (account: UphfAccount, weekNumber: number): Promise<Timetable> => { | ||
if (!account.instance) | ||
throw new ErrorServiceUnauthenticated("UPHF"); | ||
|
||
const timetable = await account.instance.getSchedule({startDate: weekNumberToDateRange(weekNumber).start.toISOString().split("T")[0], endDate:weekNumberToDateRange(weekNumber).end.toISOString().split("T")[0]}); | ||
const eventsList = timetable.plannings.flatMap((planning) => | ||
planning.events.map((event: EventResponse) => ({ | ||
id: event.id, | ||
startDateTime: event.startDateTime, | ||
endDateTime: event.endDateTime, | ||
course: event.course, | ||
rooms: event.rooms.length >= 1 ? event.rooms : [{id: null, label: null, type: null}], | ||
teachers: event.teachers.length >= 1 ? event.teachers : [{id: null, displayname: null, email: null}], | ||
group: event.groups.length >= 1 ? event.groups : [{id: null, label: null}], | ||
})) | ||
); | ||
|
||
return await eventsList.map(decodeTimetableClass); // TODO for Papillon team: add the group to the timetable | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Personalization } from "@/stores/account/types"; | ||
import { defaultTabs } from "@/consts/DefaultTabs"; | ||
import type pronote from "pawnote"; | ||
|
||
import colors from "@/utils/data/colors.json"; | ||
|
||
const defaultUphfTabs = [ | ||
"Home", | ||
"Lessons", | ||
"News", | ||
] as typeof defaultTabs[number]["tab"][]; | ||
|
||
const defaultPersonalization = async (): Promise<Partial<Personalization>> => { | ||
return { | ||
color: colors[0], | ||
magicEnabled: true, | ||
tabs: defaultTabs.filter(current => defaultUphfTabs.includes(current.tab)).map((tab, index) => ({ | ||
name: tab.tab, | ||
enabled: index <= 4 | ||
})) | ||
}; | ||
}; | ||
|
||
export default defaultPersonalization; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { UphfAccount } from "@/stores/account/types"; | ||
import type { Reconnected } from "../reload-account"; | ||
import { authWithRefreshToken } from "uphf-api"; | ||
|
||
export const reloadInstance = async (authentication: UphfAccount["authentication"]): Promise<Reconnected<UphfAccount>> => { | ||
const session = await authWithRefreshToken({ refreshAuthToken: authentication.refreshAuthToken }); | ||
|
||
return { | ||
instance: session, | ||
authentication: { | ||
refreshAuthToken: session.userData.refreshAuthToken | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useState } from "react"; | ||
import type { Screen } from "@/router/helpers/types"; | ||
|
||
import { authWithCredentials } from "uphf-api"; | ||
import uuid from "@/utils/uuid-v4"; | ||
|
||
import { useAccounts, useCurrentAccount } from "@/stores/account"; | ||
import { AccountService, type UphfAccount } from "@/stores/account/types"; | ||
import defaultPersonalization from "@/services/uphf/default-personalization"; | ||
import LoginView from "@/components/Templates/LoginView"; | ||
|
||
const UnivUphf_Login: Screen<"UnivUphf_Login"> = ({ navigation }) => { | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const createStoredAccount = useAccounts(store => store.create); | ||
const switchTo = useCurrentAccount(store => store.switchTo); | ||
|
||
const handleLogin = async (username: string, password: string) => { | ||
try { | ||
setLoading(true); | ||
setError(null); | ||
|
||
const account = await authWithCredentials({ username, password }); | ||
|
||
const local_account: UphfAccount = { | ||
instance: undefined, | ||
|
||
localID: uuid(), | ||
service: AccountService.UPHF, | ||
|
||
isExternal: false, | ||
linkedExternalLocalIDs: [], | ||
|
||
name: account.userData.name + " " + account.userData.firstname, | ||
studentName: { | ||
last: account.userData.name, | ||
first: account.userData.firstname | ||
}, | ||
className: "", // TODO ? | ||
schoolName: "Université Polytechnique Hauts-de-France", | ||
|
||
authentication: { | ||
refreshAuthToken: account.userData.refreshAuthToken, | ||
}, | ||
personalization: await defaultPersonalization() | ||
}; | ||
|
||
createStoredAccount(local_account); | ||
setLoading(false); | ||
switchTo(local_account); | ||
|
||
// We need to wait a tick to make sure the account is set before navigating. | ||
queueMicrotask(() => { | ||
// Reset the navigation stack to the "Home" screen. | ||
// Prevents the user from going back to the login screen. | ||
navigation.reset({ | ||
index: 0, | ||
routes: [{ name: "AccountCreated" }], | ||
}); | ||
}); | ||
} | ||
catch (error) { | ||
if (error instanceof Error) { | ||
setError(error.message); | ||
} | ||
else { | ||
setError("Erreur inconnue"); | ||
} | ||
|
||
setLoading(false); | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<LoginView | ||
serviceIcon={require("@/../assets/images/service_uphf.png")} | ||
serviceName="Université Polytechnique Hauts-de-France" | ||
loading={loading} | ||
error={error} | ||
onLogin={(username, password) => handleLogin(username, password)} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default UnivUphf_Login; |