@@ -160,6 +160,104 @@ export const addNote = async (trackedInternshipId, message) => {
160160 return newNote ;
161161} ;
162162
163+ /**
164+ * Get all activities from a tracked internship
165+ * @param trackedInternshipId tracked internship id
166+ * @returns an array of activities
167+ */
168+ export const getActivitiesByTrackedInternshipId = async (
169+ trackedInternshipId
170+ ) => {
171+ const trackedInternship = mockTrackerData . find (
172+ ( trackedInternship ) => trackedInternship . id === trackedInternshipId
173+ ) ;
174+
175+ if ( ! trackedInternship )
176+ throw new Error (
177+ `No tracked internship associated with id: ${ trackedInternshipId } .`
178+ ) ;
179+
180+ // Sort activity in chronological order
181+ const sortedActivity = [ ...trackedInternship . activity ] . sort ( ( a , b ) => {
182+ const timeA = a . date ? new Date ( a . date ) . getTime ( ) : Infinity ;
183+ const timeB = b . date ? new Date ( b . date ) . getTime ( ) : Infinity ;
184+ return timeA - timeB ;
185+ } ) ;
186+
187+ return sortedActivity ;
188+ } ;
189+
190+ /**
191+ * Add an activity to a tracked internship
192+ * @param trackedInternshipId tracked internship id
193+ * @param title activity title
194+ * @param date activity date (can be null)
195+ * @returns newly created activity
196+ */
197+ export const addActivity = async ( trackedInternshipId , title , date = null ) => {
198+ const trackedInternship = mockTrackerData . find (
199+ ( ti ) => ti . id === trackedInternshipId
200+ ) ;
201+
202+ if ( ! trackedInternship )
203+ throw new Error (
204+ `No tracked internship found with id: ${ trackedInternshipId } `
205+ ) ;
206+
207+ const newActivityId =
208+ trackedInternship . activity . length > 0
209+ ? Math . max ( ...trackedInternship . activity . map ( ( a ) => a . id ) ) + 1
210+ : 0 ;
211+
212+ const newActivity = { id : newActivityId , title, date } ;
213+ trackedInternship . activity . push ( newActivity ) ;
214+ return newActivity ;
215+ } ;
216+
217+ /**
218+ * Edit an activity by activity ID
219+ * @param activityId activity id
220+ * @param title new title
221+ * @param date new date
222+ * @returns updated activity
223+ */
224+ export const editActivity = async ( activityId , title , date ) => {
225+ let activityFound = null ;
226+
227+ mockTrackerData . forEach ( ( trackedInternship ) => {
228+ const activity = trackedInternship . activity . find (
229+ ( a ) => a . id === activityId
230+ ) ;
231+ if ( activity ) {
232+ activity . title = title ;
233+ activity . date = date ;
234+ activityFound = activity ;
235+ }
236+ } ) ;
237+
238+ if ( ! activityFound )
239+ throw new Error ( `No activity found with id: ${ activityId } ` ) ;
240+ return activityFound ;
241+ } ;
242+
243+ /**
244+ * Delete an activity by activity ID
245+ * @param activityId activity id
246+ */
247+ export const deleteActivity = async ( activityId ) => {
248+ let deleted = false ;
249+
250+ for ( const tracked of mockTrackerData ) {
251+ const index = tracked . activity . findIndex ( ( act ) => act . id === activityId ) ;
252+ if ( index !== - 1 ) {
253+ tracked . activity . splice ( index , 1 ) ;
254+ return ;
255+ }
256+ }
257+
258+ if ( ! deleted ) throw new Error ( `No activity found with id: ${ activityId } ` ) ;
259+ } ;
260+
163261/**
164262 * Fetch user account information to be displayed on account settings page
165263 * @returns user
0 commit comments