Skip to content

Commit b390ab5

Browse files
committed
ran linter and small error catching fix
1 parent ebbd2b9 commit b390ab5

File tree

4 files changed

+102
-97
lines changed

4 files changed

+102
-97
lines changed

backend/typescript/middlewares/validators/activityValidators.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const activityUserPatchValidator = async (
145145
if (!validatePrimitive(body.userId, "integer")) {
146146
return res.status(400).send(getApiValidationError("userId", "integer"));
147147
}
148-
148+
149149
return next();
150150
};
151151

@@ -157,9 +157,11 @@ export const activityScheduledTimePatchValidator = async (
157157
const { body } = req;
158158

159159
if (!validateDate(body.scheduledStartTime)) {
160-
return res.status(400).send(getApiValidationError("scheduledStartTime", "Date"));
160+
return res
161+
.status(400)
162+
.send(getApiValidationError("scheduledStartTime", "Date"));
161163
}
162-
164+
163165
return next();
164166
};
165167

@@ -173,7 +175,7 @@ export const activityStartTimePatchValidator = async (
173175
if (!validateDate(body.startTime)) {
174176
return res.status(400).send(getApiValidationError("startTime", "Date"));
175177
}
176-
178+
177179
return next();
178180
};
179181

@@ -187,7 +189,7 @@ export const activityEndTimePatchValidator = async (
187189
if (!validateDate(body.endTime)) {
188190
return res.status(400).send(getApiValidationError("endTime", "Date"));
189191
}
190-
192+
191193
return next();
192194
};
193195

@@ -197,11 +199,9 @@ export const activityNotesPatchValidator = async (
197199
next: NextFunction,
198200
) => {
199201
const { body } = req;
200-
if (
201-
!validatePrimitive(body.notes, "string")
202-
) {
202+
if (!validatePrimitive(body.notes, "string")) {
203203
return res.status(400).send(getApiValidationError("notes", "string"));
204204
}
205205

206206
return next();
207-
};
207+
};

backend/typescript/rest/activityRoutes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ activityRouter.patch(
167167
try {
168168
const { body } = req;
169169
const Activity = await activityService.scheduleActivity(id, {
170-
time: body.scheduledStartTime
170+
time: body.scheduledStartTime,
171171
});
172172
res.status(200).json(Activity);
173173
} catch (e: unknown) {
@@ -185,7 +185,7 @@ activityRouter.patch(
185185
try {
186186
const { body } = req;
187187
const Activity = await activityService.startActivity(id, {
188-
time: body.startTime
188+
time: body.startTime,
189189
});
190190
res.status(200).json(Activity);
191191
} catch (e: unknown) {
@@ -203,7 +203,7 @@ activityRouter.patch(
203203
try {
204204
const { body } = req;
205205
const Activity = await activityService.endActivity(id, {
206-
time: body.endTime
206+
time: body.endTime,
207207
});
208208
res.status(200).json(Activity);
209209
} catch (e: unknown) {
@@ -221,7 +221,7 @@ activityRouter.patch(
221221
try {
222222
const { body } = req;
223223
const Activity = await activityService.updateActivityNotes(id, {
224-
notes: body.notes
224+
notes: body.notes,
225225
});
226226
res.status(200).json(Activity);
227227
} catch (e: unknown) {

backend/typescript/services/implementations/activityService.ts

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ class ActivityService implements IActivityService {
6767
try {
6868
const activities: Array<PgActivity> = await PgActivity.findAll({
6969
where: {
70-
pet_id: pet_id,
70+
pet_id,
7171
},
7272
raw: true,
7373
});
74+
if (!activities[0]) {
75+
throw new NotFoundError(`No activities for pet id ${pet_id}`);
76+
}
7477
return activities.map((activity) => ({
7578
id: activity.id,
7679
userId: activity.user_id,
@@ -89,14 +92,19 @@ class ActivityService implements IActivityService {
8992
}
9093
}
9194

92-
async getUserActivities(user_id: string): Promise<Array<ActivityResponseDTO>> {
95+
async getUserActivities(
96+
user_id: string,
97+
): Promise<Array<ActivityResponseDTO>> {
9398
try {
9499
const activities: Array<PgActivity> = await PgActivity.findAll({
95100
where: {
96-
user_id: user_id,
101+
user_id,
97102
},
98103
raw: true,
99104
});
105+
if (!activities[0]) {
106+
throw new NotFoundError(`No activities for user id ${user_id}`);
107+
}
100108
return activities.map((activity) => ({
101109
id: activity.id,
102110
userId: activity.user_id,
@@ -115,7 +123,6 @@ class ActivityService implements IActivityService {
115123
}
116124
}
117125

118-
119126
async createActivity(
120127
activity: ActivityRequestDTO,
121128
): Promise<ActivityResponseDTO> {
@@ -192,7 +199,7 @@ class ActivityService implements IActivityService {
192199

193200
async assignUser(
194201
id: string,
195-
user: ActivityUserPatchDTO
202+
user: ActivityUserPatchDTO,
196203
): Promise<ActivityResponseDTO | null> {
197204
let resultingActivity: PgActivity | null;
198205
let updateResult: [number, PgActivity[]] | null;
@@ -228,7 +235,7 @@ class ActivityService implements IActivityService {
228235

229236
async scheduleActivity(
230237
id: string,
231-
schedule: ActivityTimePatchDTO
238+
schedule: ActivityTimePatchDTO,
232239
): Promise<ActivityResponseDTO | null> {
233240
let resultingActivity: PgActivity | null;
234241
let updateResult: [number, PgActivity[]] | null;
@@ -261,84 +268,82 @@ class ActivityService implements IActivityService {
261268
notes: resultingActivity.notes,
262269
};
263270
}
264-
265-
271+
266272
async startActivity(
267-
id: string,
268-
startTime: ActivityTimePatchDTO
269-
): Promise<ActivityResponseDTO | null> {
270-
let resultingActivity: PgActivity | null;
271-
let updateResult: [number, PgActivity[]] | null;
272-
try {
273-
updateResult = await PgActivity.update(
274-
{
275-
start_time: startTime.time,
276-
},
277-
{ where: { id }, returning: true },
278-
);
279-
280-
if (!updateResult[0]) {
281-
throw new NotFoundError(`Activity id ${id} not found`);
282-
}
283-
[, [resultingActivity]] = updateResult;
284-
} catch (error: unknown) {
285-
Logger.error(
286-
`Failed to update activity. Reason = ${getErrorMessage(error)}`,
287-
);
288-
throw error;
273+
id: string,
274+
startTime: ActivityTimePatchDTO,
275+
): Promise<ActivityResponseDTO | null> {
276+
let resultingActivity: PgActivity | null;
277+
let updateResult: [number, PgActivity[]] | null;
278+
try {
279+
updateResult = await PgActivity.update(
280+
{
281+
start_time: startTime.time,
282+
},
283+
{ where: { id }, returning: true },
284+
);
285+
286+
if (!updateResult[0]) {
287+
throw new NotFoundError(`Activity id ${id} not found`);
289288
}
290-
return {
291-
id: resultingActivity.id,
292-
userId: resultingActivity.user_id,
293-
petId: resultingActivity.pet_id,
294-
activityTypeId: resultingActivity.activity_type_id,
295-
scheduledStartTime: resultingActivity.scheduled_start_time,
296-
startTime: resultingActivity.start_time,
297-
endTime: resultingActivity.end_time,
298-
notes: resultingActivity.notes,
299-
};
289+
[, [resultingActivity]] = updateResult;
290+
} catch (error: unknown) {
291+
Logger.error(
292+
`Failed to update activity. Reason = ${getErrorMessage(error)}`,
293+
);
294+
throw error;
300295
}
301-
296+
return {
297+
id: resultingActivity.id,
298+
userId: resultingActivity.user_id,
299+
petId: resultingActivity.pet_id,
300+
activityTypeId: resultingActivity.activity_type_id,
301+
scheduledStartTime: resultingActivity.scheduled_start_time,
302+
startTime: resultingActivity.start_time,
303+
endTime: resultingActivity.end_time,
304+
notes: resultingActivity.notes,
305+
};
306+
}
307+
302308
async endActivity(
303-
id: string,
304-
endTime: ActivityTimePatchDTO
305-
): Promise<ActivityResponseDTO | null> {
306-
let resultingActivity: PgActivity | null;
307-
let updateResult: [number, PgActivity[]] | null;
308-
try {
309-
updateResult = await PgActivity.update(
310-
{
311-
end_time: endTime.time,
312-
},
313-
{ where: { id }, returning: true },
314-
);
315-
316-
if (!updateResult[0]) {
317-
throw new NotFoundError(`Activity id ${id} not found`);
318-
}
319-
[, [resultingActivity]] = updateResult;
320-
} catch (error: unknown) {
321-
Logger.error(
322-
`Failed to update activity. Reason = ${getErrorMessage(error)}`,
323-
);
324-
throw error;
309+
id: string,
310+
endTime: ActivityTimePatchDTO,
311+
): Promise<ActivityResponseDTO | null> {
312+
let resultingActivity: PgActivity | null;
313+
let updateResult: [number, PgActivity[]] | null;
314+
try {
315+
updateResult = await PgActivity.update(
316+
{
317+
end_time: endTime.time,
318+
},
319+
{ where: { id }, returning: true },
320+
);
321+
322+
if (!updateResult[0]) {
323+
throw new NotFoundError(`Activity id ${id} not found`);
325324
}
326-
return {
327-
id: resultingActivity.id,
328-
userId: resultingActivity.user_id,
329-
petId: resultingActivity.pet_id,
330-
activityTypeId: resultingActivity.activity_type_id,
331-
scheduledStartTime: resultingActivity.scheduled_start_time,
332-
startTime: resultingActivity.start_time,
333-
endTime: resultingActivity.end_time,
334-
notes: resultingActivity.notes,
335-
};
325+
[, [resultingActivity]] = updateResult;
326+
} catch (error: unknown) {
327+
Logger.error(
328+
`Failed to update activity. Reason = ${getErrorMessage(error)}`,
329+
);
330+
throw error;
336331
}
337-
338-
332+
return {
333+
id: resultingActivity.id,
334+
userId: resultingActivity.user_id,
335+
petId: resultingActivity.pet_id,
336+
activityTypeId: resultingActivity.activity_type_id,
337+
scheduledStartTime: resultingActivity.scheduled_start_time,
338+
startTime: resultingActivity.start_time,
339+
endTime: resultingActivity.end_time,
340+
notes: resultingActivity.notes,
341+
};
342+
}
343+
339344
async updateActivityNotes(
340345
id: string,
341-
notes: ActivityNotesPatchDTO
346+
notes: ActivityNotesPatchDTO,
342347
): Promise<ActivityResponseDTO | null> {
343348
let resultingActivity: PgActivity | null;
344349
let updateResult: [number, PgActivity[]] | null;

backend/typescript/services/interfaces/activityService.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface IActivityService {
5656
*/
5757
getPetActivities(pet_id: string): Promise<Array<ActivityResponseDTO>>;
5858

59-
/**
59+
/**
6060
* retrieve all Activities for a specific user
6161
* @param user_id user id
6262
* @returns returns array of Activities
@@ -93,7 +93,7 @@ export interface IActivityService {
9393
*/
9494
assignUser(
9595
id: string,
96-
user: ActivityUserPatchDTO
96+
user: ActivityUserPatchDTO,
9797
): Promise<ActivityResponseDTO | null>;
9898

9999
/**
@@ -104,9 +104,9 @@ export interface IActivityService {
104104
* @throws Error if update fails
105105
*/
106106
scheduleActivity(
107-
id: string,
108-
schedule: ActivityTimePatchDTO
109-
): Promise<ActivityResponseDTO | null>;
107+
id: string,
108+
schedule: ActivityTimePatchDTO,
109+
): Promise<ActivityResponseDTO | null>;
110110

111111
/**
112112
* starts an activity by adding a start time
@@ -117,7 +117,7 @@ export interface IActivityService {
117117
*/
118118
startActivity(
119119
id: string,
120-
startTime: ActivityTimePatchDTO
120+
startTime: ActivityTimePatchDTO,
121121
): Promise<ActivityResponseDTO | null>;
122122

123123
/**
@@ -129,7 +129,7 @@ export interface IActivityService {
129129
*/
130130
endActivity(
131131
id: string,
132-
endTime: ActivityTimePatchDTO
132+
endTime: ActivityTimePatchDTO,
133133
): Promise<ActivityResponseDTO | null>;
134134

135135
/**
@@ -141,7 +141,7 @@ export interface IActivityService {
141141
*/
142142
updateActivityNotes(
143143
id: string,
144-
notes: ActivityNotesPatchDTO
144+
notes: ActivityNotesPatchDTO,
145145
): Promise<ActivityResponseDTO | null>;
146146

147147
/**

0 commit comments

Comments
 (0)