Skip to content

Commit fe2270d

Browse files
committed
fix: update animal hours trained after adding train log
1 parent 3f67e6d commit fe2270d

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

‎e2e/animals.spec.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ test('create a new animal', async ({ page }) => {
2525
.fill('5');
2626

2727
await page.getByRole('button', { name: 'Save' }).click();
28-
await expect(page.getByText('Animal created successfully!')).toBeVisible();
2928

3029
await page.getByRole('link', { name: 'Animals icon Animals' }).click();
3130
await expect(page.getByText('Rover - German Shepherd')).toBeVisible();

‎e2e/train-logs.spec.ts‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import test, { expect } from '@playwright/test';
22
test.describe.serial('training log tests ', () => {
3-
test.beforeAll(async ({ browser }) => {
4-
const page = await browser.newPage();
3+
test('create a new training log', async ({ page }) => {
54
await page.goto('/');
65
// Expect a title "to contain" a substring.
76
await page.getByRole('link', { name: 'app logo Progress' }).click();
@@ -13,6 +12,7 @@ test.describe.serial('training log tests ', () => {
1312
await page.getByRole('textbox', { name: 'Email' }).press('Tab');
1413
await page.getByRole('textbox', { name: 'Password' }).fill('12345678');
1514
await page.getByRole('button', { name: 'Log In' }).click();
15+
1616
await page.getByRole('link', { name: 'Animals icon Animals' }).click();
1717
await page
1818
.getByRole('link', { name: 'create new animal Create New' })
@@ -25,23 +25,9 @@ test.describe.serial('training log tests ', () => {
2525
.fill('5');
2626

2727
await page.getByRole('button', { name: 'Save' }).click();
28-
await expect(
29-
page.getByText('Animal created successfully!')
30-
).toBeVisible();
31-
});
32-
33-
test('create a new training log', async ({ page }) => {
34-
await page.goto('/');
35-
// Expect a title "to contain" a substring.
36-
await page.getByRole('link', { name: 'app logo Progress' }).click();
37-
await page.getByRole('link', { name: 'Log In' }).click();
38-
await page.getByRole('textbox', { name: 'Email' }).click();
3928
await page
40-
.getByRole('textbox', { name: 'Email' })
41-
.fill('alice3@gatech.edu');
42-
await page.getByRole('textbox', { name: 'Email' }).press('Tab');
43-
await page.getByRole('textbox', { name: 'Password' }).fill('12345678');
44-
await page.getByRole('button', { name: 'Log In' }).click();
29+
.getByRole('link', { name: 'Training Logs icon Training' })
30+
.click();
4531
await page
4632
.getByRole('link', { name: 'create new training log' })
4733
.click();
@@ -56,7 +42,6 @@ test.describe.serial('training log tests ', () => {
5642
.getByRole('textbox', { name: 'Description' })
5743
.fill('Rover played fetch and returned the ball every time.');
5844
await page.getByRole('button', { name: 'Save' }).click();
59-
await expect(page.getByText('Training log created')).toBeVisible();
6045

6146
await page
6247
.getByRole('link', { name: 'Training Logs icon Training' })
@@ -89,9 +74,6 @@ test.describe.serial('training log tests ', () => {
8974
.getByRole('textbox', { name: 'Title' })
9075
.fill('Running Practice');
9176
await page.getByRole('button', { name: 'Save' }).click();
92-
await expect(page.locator('[id="1"]')).toContainText(
93-
'Training log updated successfully.'
94-
);
9577
await page
9678
.getByRole('link', { name: 'Training Logs icon Training' })
9779
.click();

‎server/mongodb/actions/updateAnimal.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import connectDB from '..';
2-
import { Animal, AnimalDocument, AnimalType } from '../models/Animal';
2+
import { Animal, AnimalDocument } from '../models/Animal';
33

4-
5-
const updateAnimal = async (data: Partial<AnimalType> & {_id: string}): Promise<AnimalDocument | null> => {
6-
7-
const {_id} = data;
8-
try{
4+
const updateAnimal = async (
5+
data: Partial<AnimalDocument>
6+
): Promise<AnimalDocument | null> => {
7+
const { _id } = data;
8+
try {
99
await connectDB();
10-
let animal = await Animal.findByIdAndUpdate(_id, data, {new: true}).exec();
10+
let animal = await Animal.findByIdAndUpdate(_id, data, {
11+
new: true,
12+
}).exec();
1113
if (!animal) {
12-
throw new Error(`Cannot find animal with ID ${_id}`)
14+
throw new Error(`Cannot find animal with ID ${_id}`);
1315
}
1416
return animal;
1517
} catch (err) {
16-
console.error(`[ERROR]: Error encountered while creating Animal: ${err}`);
18+
console.error(
19+
`[ERROR]: Error encountered while creating Animal: ${err}`
20+
);
1721
return null;
1822
}
1923
};

‎src/app/api/training-log/[id]/route.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { NextRequest } from 'next/server';
22
import updateTrainingLog from '../../../../../server/mongodb/actions/updateTrainingLog';
33
import { auth } from '@/lib/auth';
44
import findTrainingLogById from '../../../../../server/mongodb/actions/findTrainingLogById';
5+
import findAnimalById from '../../../../../server/mongodb/actions/findAnimalById';
6+
import updateAnimal from '../../../../../server/mongodb/actions/updateAnimal';
57

68
export const GET = async (
79
req: NextRequest,
@@ -67,6 +69,14 @@ export const PATCH = async (req: NextRequest): Promise<Response> => {
6769
data.date = dateObj;
6870
data.user = userId;
6971

72+
const oldData = await findTrainingLogById(data._id);
73+
const animal = await findAnimalById(data.animal);
74+
75+
if (animal && oldData) {
76+
animal.hoursTrained = animal?.hoursTrained - oldData?.hours;
77+
await updateAnimal(animal);
78+
}
79+
7080
const trainingLog = await updateTrainingLog(data);
7181

7282
if (!trainingLog) {

‎src/app/api/training-log/route.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TrainingLogDocument } from '../../../../server/mongodb/models/TrainingL
66
import findAnimalById from '../../../../server/mongodb/actions/findAnimalById';
77
import createTrainingLog from '../../../../server/mongodb/actions/createTrainingLog';
88
import { auth } from '@/lib/auth';
9+
import updateAnimal from '../../../../server/mongodb/actions/updateAnimal';
910

1011
export const GET = async (req: NextRequest): Promise<Response> => {
1112
const session = await auth();
@@ -94,5 +95,10 @@ export const POST = async (req: NextRequest): Promise<Response> => {
9495

9596
const newLog = await createTrainingLog(data);
9697

98+
if (newLog) {
99+
animalObj.hoursTrained = animalObj?.hoursTrained + newLog?.hours;
100+
await updateAnimal(animalObj);
101+
}
102+
97103
return new Response(JSON.stringify({ data: newLog }), { status: 200 });
98104
};

0 commit comments

Comments
 (0)