Skip to content

Commit

Permalink
feat: usuario: get and update imagem perfil and imagem capa
Browse files Browse the repository at this point in the history
  • Loading branch information
guesant committed Mar 29, 2024
1 parent c5d367b commit b8d6449
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IAmbienteModel } from '..';
import { IDatedObject, IEntityDate, IObjectUuid } from '../../(core)';
import { IImagemModel } from '../../base/imagem';
import { IAmbienteModel } from '../ambiente';
import { ICampusModel } from '../campus';

export interface IBlocoModel extends IObjectUuid, IDatedObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ICampusModel } from '../../../../ambientes';
import { IImagemFindOneResultDto } from '../../../../base';
import { IUsuarioVinculoCampusModel } from '../../../usuario-vinculo-campus';
import { IUsuarioModel } from '../../IUsuarioModel';

Expand All @@ -10,4 +11,7 @@ export interface IUsuarioFindOneResultDtoVinculoAtivo extends Pick<IUsuarioVincu

export interface IUsuarioFindOneResultDto extends Pick<IUsuarioModel, 'id' | 'nome' | 'matriculaSiape' | 'email'> {
vinculosAtivos: IUsuarioFindOneResultDtoVinculoAtivo[];

imagemCapa: IImagemFindOneResultDto | null;
imagemPerfil: IImagemFindOneResultDto | null;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDatedObject, IEntityDate, IObjectUuid } from '../../(core)';
import * as Dto from '../../(core)';
import { IImagemModel } from '../../base';

export interface IUsuarioModel extends IObjectUuid, IDatedObject {
export interface IUsuarioModel extends Dto.IObjectUuid, Dto.IDatedObject {
//

id: string;
Expand All @@ -17,9 +18,14 @@ export interface IUsuarioModel extends IObjectUuid, IDatedObject {

//

dateCreated: IEntityDate;
dateUpdated: IEntityDate;
dateDeleted: null | IEntityDate;
imagemCapa: IImagemModel | null;
imagemPerfil: IImagemModel | null;

//

dateCreated: Dto.IEntityDate;
dateUpdated: Dto.IEntityDate;
dateDeleted: null | Dto.IEntityDate;

//
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ export class BlocoController {
return this.blocoService.blocoFindByIdStrict(contextoDeAcesso, { id });
}

@Get('/:id/imagem/capa')
@ApiProduces('application/octet-stream', 'image/jpeg')
@DtoOperationFindOne(BlocoOperations.BLOCO_GET_IMAGEM_CAPA)
async blocoGetImagemCapa(
@ContextoDeAcessoHttp() contextoDeAcesso: IContextoDeAcesso,
@HttpDtoParam(BlocoOperations.BLOCO_GET_IMAGEM_CAPA, 'id')
id: string,
) {
return this.blocoService.blocoGetImagemCapa(contextoDeAcesso, id);
}

//

@Post('/')
Expand Down Expand Up @@ -81,6 +70,19 @@ export class BlocoController {
return this.blocoService.blocoUpdate(contextoDeAcesso, dtoUpdate);
}

//

@Get('/:id/imagem/capa')
@ApiProduces('application/octet-stream', 'image/jpeg')
@DtoOperationFindOne(BlocoOperations.BLOCO_GET_IMAGEM_CAPA)
async blocoGetImagemCapa(
@ContextoDeAcessoHttp() contextoDeAcesso: IContextoDeAcesso,
@HttpDtoParam(BlocoOperations.BLOCO_GET_IMAGEM_CAPA, 'id')
id: string,
) {
return this.blocoService.blocoGetImagemCapa(contextoDeAcesso, id);
}

@Put('/:id/imagem/capa')
@ApiConsumes('multipart/form-data')
@ApiBody({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ export class BlocoService {
//

static BlocoQueryBuilderView(alias: string, qb: SelectQueryBuilder<any>, options: IBlocoQueryBuilderViewOptions = {}) {
const loadCampus = getQueryBuilderViewLoadMeta(options.loadCampus, true, `${alias}_campus`);
const loadImagemCapa = getQueryBuilderViewLoadMeta(options.loadImagemCapa, true, `${alias}_imagemCapa`);

qb.addSelect([
//
`${alias}.id`,
`${alias}.nome`,
`${alias}.codigo`,
]);

const loadCampus = getQueryBuilderViewLoadMeta(options.loadCampus, true, `${alias}_campus`);

if (loadCampus) {
qb.leftJoin(`${alias}.campus`, `${loadCampus.alias}`);
CampusService.CampusQueryBuilderView(loadCampus.alias, qb, loadCampus.options);
}

const loadImagemCapa = getQueryBuilderViewLoadMeta(options.loadImagemCapa, true, `${alias}_imagemCapa`);

if (loadImagemCapa) {
qb.leftJoin(`${alias}.imagemCapa`, `${loadImagemCapa.alias}`);
ImagemService.ImagemQueryBuilderView(loadImagemCapa.alias, qb, loadImagemCapa.options);
Expand Down Expand Up @@ -256,6 +257,36 @@ export class BlocoService {
throw new NotFoundException();
}

async blocoUpdateImagemCapa(contextoDeAcesso: IContextoDeAcesso, dto: Dtos.IBlocoFindOneByIdInputDto, file: Express.Multer.File) {
// =========================================================

const currentBloco = await this.blocoFindByIdStrict(contextoDeAcesso, { id: dto.id });

// =========================================================

await contextoDeAcesso.ensurePermission('bloco:update', { dto: { id: currentBloco.id } }, currentBloco.id, this.blocoRepository.createQueryBuilder(aliasBloco));

// =========================================================

const { imagem } = await this.imagemService.saveBlocoCapa(file);

const bloco = <BlocoEntity>{
id: currentBloco.id,
};

this.blocoRepository.merge(bloco, {
imagemCapa: {
id: imagem.id,
},
});

await this.blocoRepository.save(bloco);

// =========================================================

return true;
}

//

async blocoCreate(contextoDeAcesso: IContextoDeAcesso, dto: Dtos.IBlocoInputDto) {
Expand Down Expand Up @@ -324,36 +355,6 @@ export class BlocoService {
return this.blocoFindByIdStrict(contextoDeAcesso, { id: bloco.id });
}

async blocoUpdateImagemCapa(contextoDeAcesso: IContextoDeAcesso, dto: Dtos.IBlocoFindOneByIdInputDto, file: Express.Multer.File) {
// =========================================================

const currentBloco = await this.blocoFindByIdStrict(contextoDeAcesso, { id: dto.id });

// =========================================================

await contextoDeAcesso.ensurePermission('bloco:update', { dto: { id: currentBloco.id } }, currentBloco.id, this.blocoRepository.createQueryBuilder(aliasBloco));

// =========================================================

const { imagem } = await this.imagemService.saveBlocoCapa(file);

const bloco = <BlocoEntity>{
id: currentBloco.id,
};

this.blocoRepository.merge(bloco, {
imagemCapa: {
id: imagem.id,
},
});

await this.blocoRepository.save(bloco);

// =========================================================

return true;
}

//

async blocoDeleteOneById(contextoDeAcesso: IContextoDeAcesso, dto: Dtos.IBlocoDeleteOneByIdInputDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectType } from '@nestjs/graphql';
import { PickType } from '@nestjs/swagger';
import { ICampusFindOneResultDto, IUsuarioFindOneResultDto, IUsuarioFindOneResultDtoVinculoAtivo } from '../../../(spec)';
import { ICampusFindOneResultDto, IImagemModel, IUsuarioFindOneResultDto, IUsuarioFindOneResultDtoVinculoAtivo } from '../../../(spec)';
import { DtoProperty, createDtoPropertyMap } from '../../../../../infrastructure';
import { CampusFindOneResultDto } from '../../../ambientes/campus/dtos';
import { UsuarioVinculoCampusDto, UsuarioVinculoCampusDtoProperties, UsuarioVinculoCampusFindOneResultDto } from '../../usuario-vinculo-campus/dtos';
Expand Down Expand Up @@ -51,6 +51,14 @@ export class UsuarioFindOneResultDto implements IUsuarioFindOneResultDto {

//

@DtoProperty(UsuarioDtoProperties.USUARIO_IMAGEM_CAPA_OUTPUT)
imagemCapa!: IImagemModel | null;

@DtoProperty(UsuarioDtoProperties.USUARIO_IMAGEM_PERFIL_OUTPUT)
imagemPerfil!: IImagemModel | null;

//

@DtoProperty(UsuarioFindOneDtoProperties.USUARIO_VINCULOS_ATIVOS_OUTPUT)
vinculosAtivos!: IUsuarioFindOneResultDtoVinculoAtivo[];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ValidationContractUuid, createDtoOperationOptions } from '../../../../../infrastructure';

// ======================================================

export const USUARIO_GET_IMAGEM_CAPA = createDtoOperationOptions({
description: 'Obtêm a imagem de capa do usuário.',

gql: null,

swagger: {
returnType: {
schema: {
type: 'string',
format: 'binary',
},
},

params: [
{
name: 'id',
description: 'ID do bloco.',
validationContract: ValidationContractUuid,
},
],
},
});

// ======================================================
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ValidationContractUuid, createDtoOperationOptions } from '../../../../../infrastructure';

// ======================================================

export const USUARIO_GET_IMAGEM_PERFIL = createDtoOperationOptions({
description: 'Obtêm a imagem de perfil do usuário.',

gql: null,

swagger: {
returnType: {
schema: {
type: 'string',
format: 'binary',
},
},

params: [
{
name: 'id',
description: 'ID do bloco.',
validationContract: ValidationContractUuid,
},
],
},
});

// ======================================================
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ObjectType } from '@nestjs/graphql';
import * as yup from 'yup';
import { IEntityDate, IUsuarioModel } from '../../../(spec)';
import { IEntityDate, IImagemModel, IUsuarioModel } from '../../../(spec)';
import { CommonPropertyUuid, DtoProperty, ValidationContractString, ValidationContractUuid, createDtoPropertyMap, createValidationContract } from '../../../../../infrastructure';
import { ImagemDto, ImagemFindOneResultDto } from '../../../base/imagem/dtos';

// ======================================================

Expand Down Expand Up @@ -61,6 +62,28 @@ export const UsuarioDtoProperties = createDtoPropertyMap({
},

//
USUARIO_IMAGEM_CAPA_OUTPUT: {
nullable: true,
description: 'Imagem de capa do usuário.',
//
gql: {
type: () => ImagemDto,
},
swagger: {
type: ImagemFindOneResultDto,
},
},
USUARIO_IMAGEM_PERFIL_OUTPUT: {
nullable: true,
description: 'Imagem de perfil do usuário.',
//
gql: {
type: () => ImagemDto,
},
swagger: {
type: ImagemFindOneResultDto,
},
},
});

// ======================================================
Expand All @@ -83,8 +106,18 @@ export class UsuarioDto implements IUsuarioModel {

//

@DtoProperty(UsuarioDtoProperties.USUARIO_IMAGEM_CAPA_OUTPUT)
imagemCapa!: IImagemModel | null;

@DtoProperty(UsuarioDtoProperties.USUARIO_IMAGEM_PERFIL_OUTPUT)
imagemPerfil!: IImagemModel | null;

//

isSuperUser!: boolean;

//

dateCreated!: IEntityDate;
dateUpdated!: IEntityDate;
dateDeleted!: IEntityDate | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import {
} from '.';
import { ValidationContractUuid, createDtoOperationOptions, createValidationContract } from '../../../../../infrastructure';
import { UsuarioFindOneResultDto } from './usuario-find-one.result.dto';
import { USUARIO_GET_IMAGEM_CAPA } from './usuario-get-imagem-capa.operation';
import { USUARIO_GET_IMAGEM_PERFIL } from './usuario-get-imagem-perfil.operation';
import { UsuarioDto } from './usuario.dto';

export const UsuarioOperations = {
// ===============================
USUARIO_GET_IMAGEM_CAPA: USUARIO_GET_IMAGEM_CAPA,
USUARIO_GET_IMAGEM_PERFIL: USUARIO_GET_IMAGEM_PERFIL,
// ===============================

USUARIO_FIND_ALL: createDtoOperationOptions({
description: 'Lista de todos os usuários cadastrados no sistema.',

Expand Down
Loading

0 comments on commit b8d6449

Please sign in to comment.