Skip to content

Commit

Permalink
project finished b00tc4mp#234
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctgenix committed Jan 21, 2025
1 parent 692c096 commit 41afbe2
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default (username, password) => {
throw new SystemError(error.message)
}

if (!user) throw new CredentialsError('wrong credentials')
if (!user) throw new CredentialsError('Usuario o contraseña incorrectos')

let match

Expand All @@ -27,7 +27,7 @@ export default (username, password) => {
throw new SystemError(error.message)
}

if (!match) throw new CredentialsError('wrong credentials')
if (!match) throw new CredentialsError('Usuario o contraseña incorrectos')

return {
id: user._id.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ describe('authenticateUser', () => {
expect(
User.create({ name: 'Carlos Tomas', username: 'ctcarlos25', password: bcrypt.hashSync('123123123', 10), phone: '+34682519205', email: 'ctcarlos25@gmail.com', passwordRepeat: '123123123' })
.then(() => authenticateUser('ctcarlos25', '12312312'))
).to.be.rejectedWith(CredentialsError, 'wrong credentials')
).to.be.rejectedWith(CredentialsError, 'Usuario o contraseña incorrectos')
)


it('fails on non-existing user', () =>
expect(
authenticateUser('ctcarlos25', '123123123')
).to.be.rejectedWith(CredentialsError, 'wrong credentials')
).to.be.rejectedWith(CredentialsError, 'Usuario o contraseña incorrectos')
)
after(() => db.disconnect())
})
47 changes: 47 additions & 0 deletions staff/carlos-tomas/project/api/logic/users/getUserPets.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dotenv/config'

import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import bcrypt from 'bcryptjs'

chai.use(chaiAsPromised)
const { expect } = chai

import db, { User } from 'dat'
import { errors } from 'com'

const { NotFoundError } = errors

import getUserPets from './getUserPets.js'

debugger

describe('getUserPets', () => {
before(() => db.connect(process.env.MONGO_URL_TEST))

beforeEach(() => User.deleteMany())

it('get user pets', () => {
User.create({ name: 'Carlos Tomas', username: 'ctcarlos25', password: bcrypt.hashSync('123123123', 10), phone: '+34682519205', email: 'ctcarlos25@gmail.com' })
.then((user) =>
getUserPets(user.id)
.then(user => {
expect(user[0].id).to.equal(user.id)
expect(user[0].name).to.equal(user.name)
expect(user[0].username).to.equal(user.username)
expect(user[0].phone).to.equal(user.phone)
expect(user[0].email).to.equal(user.email)
})
)

})

it('fails on non-existing user', () =>
expect(
getUserPets('012345678901234567890123')
).to.be.rejectedWith(NotFoundError, 'user not found')
)


after(() => db.disconnect())
})
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ describe('getHistoriesPets', () => {

expect(histories[0].id).to.equal(history.id)
expect(histories[0].type).to.equal("internal_medicine")
expect(histories[0].veterinary.id).to.equal(user.id)
expect(histories[0].pet.id).to.equal(pet.id)
expect(histories[0].text).to.equal(history.text)
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (userId, petId, vaccineName, dewornData) => {
if (vaccineName) {
const isVaccineDuplicated = pet.vaccines.some(vaccine => vaccine.name === vaccineName);
if (isVaccineDuplicated) {
throw new DuplicityError(`La vacuna "${vaccineName}" ya ha sido administarda al Animal.`)
throw new DuplicityError(`La vacuna "${vaccineName}" ya ha sido administrada al Animal.`)
}
const vaccine = { name: vaccineName }
pet.vaccines.push(vaccine)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import 'dotenv/config';
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import db, { User, Pet } from 'dat';
import { errors } from 'com';
import updateVaccinesDewornsPet from './updateVaccinesDewornsPet.js';

chai.use(chaiAsPromised);
const { expect } = chai;

const { DuplicityError, NotFoundError } = errors;

describe('updateVaccinesDewornsPet', () => {
before(() => db.connect(process.env.MONGO_URL_TEST));

beforeEach(() => Promise.all([User.deleteMany(), Pet.deleteMany()]));

it('adds a new vaccine to a pet', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [],
deworns: [],
}).save();

await updateVaccinesDewornsPet(user.id, pet.id, 'Rabies', null);

const updatedPet = await Pet.findById(pet.id);
expect(updatedPet.vaccines).to.have.lengthOf(1);
expect(updatedPet.vaccines[0].name).to.equal('Rabies');
});

it('throws DuplicityError if vaccine already exists', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [{ name: 'Rabies' }],
deworns: [],
}).save();

await expect(updateVaccinesDewornsPet(user.id, pet.id, 'Rabies', null)).to.be.rejectedWith(
DuplicityError,
'La vacuna "Rabies" ya ha sido administrada al Animal.'
);
});

it('adds a new deworn treatment', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [],
deworns: [],
}).save();

await updateVaccinesDewornsPet(user.id, pet.id, null, 'internal');

const updatedPet = await Pet.findById(pet.id);
expect(updatedPet.deworns).to.have.lengthOf(1);
expect(updatedPet.deworns[0].type).to.equal('internal');
});

it('throws DuplicityError if deworn treatment already exists', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [],
deworns: [{ type: 'internal' }],
}).save();

await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'internal')).to.be.rejectedWith(
DuplicityError,
"La desparacitación de tipo 'internal' ya está registrada."
);
});

it('throws DuplicityError when adding "both" if "external" or "internal" already exists', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [],
deworns: [{ type: 'internal' }],
}).save();

await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'both')).to.be.rejectedWith(
DuplicityError,
"No se puede agregar 'Ambas' si ya se han administrado 'external' o 'internal'."
);
});

it('throws DuplicityError when adding "external" or "internal" if "both" already exists', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
vaccines: [],
deworns: [{ type: 'both' }],
}).save();

await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'external')).to.be.rejectedWith(
DuplicityError,
"No se puede agregar 'external' porque ya está registrado 'both'."
);

await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'internal')).to.be.rejectedWith(
DuplicityError,
"No se puede agregar 'internal' porque ya está registrado 'both'."
);
});

it('fails when user does not exist', async () => {
const fakeUserId = '012345678901234567890123';
const pet = await new Pet({
chip: '012345678912345',
name: 'Peke',
race: 'Meztizo',
sex: true,
weight: 35,
sterilized: true,
dateOfBirth: new Date('2018-09-09'),
}).save();

await expect(updateVaccinesDewornsPet(fakeUserId, pet.id, 'Rabies', null)).to.be.rejectedWith(
NotFoundError,
'user not found'
);
});

it('fails when pet does not exist', async () => {
const user = await new User({
name: 'Carlos Tomas',
username: 'ctcarlos25',
password: '123123123',
phone: '+34682519205',
email: 'ctcarlos25@gmail.com',
}).save();

const fakePetId = '012345678901234567890123';

await expect(updateVaccinesDewornsPet(user.id, fakePetId, 'Rabies', null)).to.be.rejectedWith(
NotFoundError,
'pet not found'
);
});

after(() => db.disconnect());
});
2 changes: 1 addition & 1 deletion staff/carlos-tomas/project/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test-one": "mocha",
"test-inspect": "mocha --inspect-brk logic/*.spec.js",
"test-inspect-one": "mocha --inspect-brk",
"coverage": "c8 --experimental-monocart --reporter=html --reporter=text mocha logic/*.spec.js --timeout 20000"
"coverage": "c8 --experimental-monocart --reporter=html --reporter=text mocha logic/**/*.spec.js --timeout 20000"
},
"keywords": [],
"author": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export default function PetsScreenUserRegular() {
)}
<TouchableOpacity

style={petInfo.histoy}
style={petInfo.touchableOpacity}
onPress={() => {
navigation.navigate('showPetHistory', { infoPet: pet })
}}
>
<Text>🪧</Text>
<Text>Hisorial</Text>
</TouchableOpacity>
</View>
))
Expand All @@ -79,9 +79,20 @@ const petInfo = StyleSheet.create({
borderRadius: 8,
padding: 15,
marginBottom: 10,
backgroundColor: '#f9f9f9',
backgroundColor: '#dafbcc',
gap: 5
},
text: {
fontSize: 16,
},
touchableOpacity: {
backgroundColor: "#c1f1cf",
borderBottomColor: 'black',
borderWidth: 1,
borderRadius: 10,
padding: 15,
width: 85,
justifyContent: "center",
alignItems: "center",
}
})
Loading

0 comments on commit 41afbe2

Please sign in to comment.