Skip to content

Commit

Permalink
New screen public profile animal and fixes on animals list
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgehl3n committed Nov 18, 2023
1 parent 5631e03 commit b1d3c7d
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 10 deletions.
93 changes: 89 additions & 4 deletions src/controllers/AnimalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import Constants from "../../constants";
import AnimalFilters from "../filters/AnimalFilters";
import AnimalPredictionService from "../services/AnimalPredictionService";
import { Species } from "../enums/Species";
import Breed from "../database/models/Breed";
import Institution from "../database/models/Institution";
import User from "../database/models/User";
import Color from "../database/models/Color";
import BehavioralProfile from "../database/models/BehavioralProfile";
import UserRole from "../database/models/UserRole";
import { Roles } from "../enums/Roles";
const _mapRequestToData = async (req: Request) => {
let data = req.body as unknown as Animal;
data = mapData(data);
Expand Down Expand Up @@ -136,6 +143,20 @@ const _mapAnimalFiles = async (entity: Animal) => {
};


const _mapRolesSearch = async (entityView: Animal, userRoles: any, animalViewList: Object[]) => {
if (userRoles.some((x: UserRole) =>
x.idInstitution == entityView.idInstitution,
)) {
const role = userRoles.find((x: UserRole) =>
x.idInstitution == entityView.idInstitution,
);

animalViewList.push({ ...entityView.dataValues, role: role?.idRole as Roles });
}
else {
animalViewList.push(entityView);
}
};

interface SortingOptions {
'name-az': string[][];
Expand Down Expand Up @@ -298,27 +319,36 @@ export default class AnimalController {
separate: true,
limit: 1,
},
{
model: Institution,
as: 'institution',
attributes: ['name'],
},
],
...AnimalFilters.ApplyFilters(req),
offset: offset,
attributes: ['id', 'name', 'age', 'gender', 'size'],
attributes: ['id', 'name', 'age', 'gender', 'size', 'idInstitution'],
limit: Constants.resultsPerPage,
order: sortingCriteria,
});

if (entities) {
const institutionViewList: Object[] = [];
const authenticatedRequest = req as unknown as AuthenticatedRequest;
const { userRoles } = authenticatedRequest.user!;

const viewList: Object[] = [];
for (let i = 0; i < entities.length; i++) {
if (entities[i].animalImages && entities[i].animalImages.length > 0) {
entities[i].animalImages = [entities[i].animalImages[0]];
entities[i] = await _mapAnimalImages(entities[i]);
}
institutionViewList.push(entities[i])
const entityView = entities[i];
_mapRolesSearch(entityView, userRoles, viewList);
}

return res.status(200).json({
list: [
...institutionViewList,
...viewList,
],
});
}
Expand Down Expand Up @@ -364,4 +394,59 @@ export default class AnimalController {
return res.status(500).json(error).send();
}
}

public static async publicDetail(req: Request, res: Response) {
try {
let entity = await Animal.findByPk(req.params.id, {
include: [
'animalImages',
{
model: Breed,
as: 'breed',
attributes: ['name']
},
{
model: Institution,
as: 'institution',
attributes: ['name']
},
{
model: User,
as: 'adoptionUser',
attributes: ['name']
},
{
model: Color,
as: 'color',
attributes: ['name', 'class']
},
{
model: BehavioralProfile,
as: 'behavioralProfile',
attributes: ['name']
}
],
attributes: {
exclude: [
'collectionPlace',
'deathDate',
'deathDetail',
'adoptionSolictationDate',
'forwardingDate',
]
}
});

if (entity) {
entity = await _mapAnimalImages(entity);
return res.status(200).json(entity);
}

return res.status(404).json({});
}
catch (error) {
console.error(error);
return res.status(500).json(error).send();
}
}
}
36 changes: 33 additions & 3 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import BCrypt from 'bcryptjs';
import moment from 'moment';
import Interest from '../database/models/Interest';
import { Op } from 'sequelize';
import { AuthenticatedRequest } from '../..';
const _mapCitiesToData = (req: Request) => {
const formData = req.body;

Expand Down Expand Up @@ -44,9 +45,16 @@ const _mapInterestsToData = (req: Request) => {

const _mapRequestToData = async (req: Request) => {
const data = req.body as unknown as User;
const imageLogo = (req.files as Record<string, any>);
if (imageLogo && imageLogo.length > 0)
data.profileImage = imageLogo[0].buffer?.toString('base64');
const images = (req.files as Record<string, any>);

const profileImage = images.find(
(file: Express.Multer.File) =>
file.fieldname == 'profileImage'
);

if (profileImage) {
data.profileImage = profileImage.buffer?.toString('base64');
}
data.address = req.body.address as Address;
data.address.id = null

Expand Down Expand Up @@ -118,4 +126,26 @@ export default class UserController {
return res.status(500).json(error).send();
}
}
public static async detailLoggedUser(req: Request, res: Response) {
try {
const AuthenticatedRequest = req as unknown as AuthenticatedRequest;
const entity = AuthenticatedRequest.user;
if (entity?.profileImage) {
const image = await Buffer.from(entity.profileImage, 'base64').toString('ascii');
if (image) {
entity.profileImage = `data:image/png;base64,${image}`;
}
}
const entityJson = {
id: entity?.id,
name: entity?.name,
profileImage: entity?.profileImage,
}
res.status(200).json(entityJson);
}
catch (error) {
console.error(error);
return res.status(500).json(error).send();
}
}
}
8 changes: 8 additions & 0 deletions src/database/models/Animal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AnimalImage from './AnimalImage';
import TreatmentAnimal from './TreatmentAnimal';
import AnimalAttachment from './AnimalAttachment';
import AnimalPrediction from './AnimalPrediction';
import Breed from './Breed';

class Animal extends Model {
public id!: number;
Expand Down Expand Up @@ -36,9 +37,11 @@ class Animal extends Model {
public temporaryHome!: EntityTemporaryHome;
public medicineAnimal!: MedicineAnimal[];
public animalImages!: AnimalImage[];
public breed!: Breed;
public animalAttachments!: AnimalAttachment[];
public treatment!: TreatmentAnimal[];
public predictions!: AnimalPrediction[];
public castrated!: boolean;
}

Animal.init(
Expand Down Expand Up @@ -112,6 +115,11 @@ Animal.init(
type: DataTypes.INTEGER,
allowNull: true,
},
castrated: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false,
},
},
{
tableName: 'animal',
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class User extends Model {
public encriptedPassword!: string;
public birthdayDate!: Date;
public phone!: string;
public profileImage!: Blob;
public profileImage!: string | null;
public address!: Address;
public userRoles!: UserRole[];
public cities!: InterestCity[];
Expand Down
9 changes: 7 additions & 2 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ routes.get('/user/', (req, res) => {
return UserController.list(req, res)
});

routes.get('/detailUser/', (req, res) => {
return UserController.detailLoggedUser(req, res)
});

routes.get('/colors', (req, res) => {
return ColorController.list(req, res)
});
Expand Down Expand Up @@ -109,10 +113,11 @@ routes.get('/animal/', (req, res) => {
return AnimalController.list(req, res)
});

routes.get('/public/animal/:id', (req, res) => {
return AnimalController.publicDetail(req, res);
});

routes.post('/animal/prediction', upload.single('image'), (req, res) => {
return AnimalController.PredictionImage(req, res)
});


export default routes;

0 comments on commit b1d3c7d

Please sign in to comment.