Skip to content

Commit

Permalink
feat(backend): add patch of documents
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Oct 29, 2024
1 parent 5d01aed commit 53231a8
Show file tree
Hide file tree
Showing 44 changed files with 723 additions and 559 deletions.
5 changes: 5 additions & 0 deletions packages/backend/src/_common/model/app-log/LogAction.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export type LogAction =
// Docs
| "USAGERS_DOCS_UPLOAD"
| "USAGERS_DOCS_DOWNLOAD"
| "USAGERS_DOCS_DELETE"
| "USAGERS_DOCS_RENAME"
| "USAGERS_DOCS_SHARED"

///
| "EXPORT_USAGERS"
| "GET_STATS"
| "EXPORT_STATS"
Expand Down
14 changes: 0 additions & 14 deletions packages/backend/src/_common/model/usager/UsagerDoc.type.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/backend/src/_common/model/usager/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@index('./*', f => `export * from '${f.path}'`)
export * from "./cerfa";
export * from "./history";
export * from "./UsagerDoc.type";
export * from "./UsagerImport.type";
export * from "./UsagerLight.type";
29 changes: 29 additions & 0 deletions packages/backend/src/_migrations/1730216743164-auto-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { domifaConfig } from "../config";

export class AutoMigration1730216743164 implements MigrationInterface {
name = "AutoMigration1730216743164";

public async up(queryRunner: QueryRunner): Promise<void> {
if (
domifaConfig().envId === "prod" ||
domifaConfig().envId === "preprod" ||
domifaConfig().envId === "local"
) {
await queryRunner.query(
`ALTER TABLE "usager_docs" ADD "shared" boolean NOT NULL DEFAULT false`
);

await queryRunner.query(
`UPDATE "structure_doc" SET label = 'Attestation postale' where label='attestation_postale'`
);
await queryRunner.query(
`UPDATE "structure_doc" SET label = 'Courrier de radiation' where label='courrier_radiation'`
);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "usager_docs" DROP COLUMN "shared"`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { UsagerDoc } from "@domifa/common";
import { createParamDecorator, ExecutionContext } from "@nestjs/common";

export const CurrentUsagerDoc = createParamDecorator(
(_data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.usagerDoc as UsagerDoc;
}
);
1 change: 1 addition & 0 deletions packages/backend/src/auth/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./AllowUserStructureRoles.decorator";
export * from "./current-chosen-user-structure.decorator";
export * from "./current-interaction.decorator";
export * from "./current-structure-information.decorator";
export * from "./current-usager-doc.decorator";
export * from "./current-usager-note.decorator";
export * from "./current-usager.decorator";
export * from "./current-user.decorator";
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/auth/guards/CanGetUserStructure.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class CanGetUserStructureGuard implements CanActivate {
!isUUID(userUuid)
) {
appLogger.error(
`[CanGetUserStructureGuard] invalid user.Uuid or structureId`,
"[CanGetUserStructureGuard] invalid user.Uuid or structureId",
{
sentry: true,
context: { "user.Uuid": userUuid, structureId, user: r.user._id },
Expand All @@ -39,7 +39,7 @@ export class CanGetUserStructureGuard implements CanActivate {

if (!chosenUserStructure) {
appLogger.error(
`[CanGetUserStructureGuard] chosenUserStructure not found`,
"[CanGetUserStructureGuard] chosenUserStructure not found",
{
sentry: true,
context: {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/auth/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./CanGetUserStructure.guard";
export * from "./interactions.guard";
export * from "./structure-information-access.guard";
export * from "./usager-access.guard";
export * from "./usager-doc-access.guard";
export * from "./usager-note-access.guard";
4 changes: 2 additions & 2 deletions packages/backend/src/auth/guards/usager-access.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { usagerRepository } from "./../../database/services/usager/usagerRepository.service";
import {
CanActivate,
ExecutionContext,
Expand All @@ -8,6 +7,7 @@ import {
} from "@nestjs/common";

import { appLogger } from "../../util";
import { usagerRepository } from "../../database";

@Injectable()
export class UsagerAccessGuard implements CanActivate {
Expand All @@ -18,7 +18,7 @@ export class UsagerAccessGuard implements CanActivate {
typeof r.params.usagerRef === "undefined" ||
typeof r.user.structureId === "undefined"
) {
appLogger.error(`[UsagerAccessGuard] invalid usagerRef or structureId`, {
appLogger.error("[UsagerAccessGuard] invalid usagerRef or structureId", {
sentry: true,
context: {
usagerRef: r.params.usagerRef,
Expand Down
62 changes: 62 additions & 0 deletions packages/backend/src/auth/guards/usager-doc-access.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
CanActivate,
ExecutionContext,
HttpException,
HttpStatus,
Injectable,
} from "@nestjs/common";

import { appLogger } from "../../util";
import { isNumber, isUUID } from "class-validator";
import { usagerDocsRepository } from "../../database";
import { UserStructureAuthenticated } from "../../_common/model";

@Injectable()
export class UsagerDocAccessGuard implements CanActivate {
public async canActivate(context: ExecutionContext) {
const request = context.switchToHttp().getRequest();
const user = request?.user as UserStructureAuthenticated;

if (
user?.role === "simple" ||
!isUUID(request.params.docUuid) ||
!isNumber(user?.structureId)
) {
appLogger.error("[UsagerDocAccessGuard] invalid docUuid or structureId", {
sentry: true,
context: {
docUuid: request?.params?.docUuid,
structureId: user.structureId,
user: user.id,
role: user.role,
},
});
throw new HttpException("USAGER_DOC_NOT_FOUND", HttpStatus.BAD_REQUEST);
}

const docUuid = request.params.docUuid;

try {
// Todo: optimize this request, generate one request with a join
const usagerDoc = await usagerDocsRepository.findOneOrFail({
where: {
structureId: user.structureId,
uuid: docUuid,
},
});
request.usagerDoc = usagerDoc;
return request;
} catch (e) {
appLogger.error("[UsagerDocAccessGuard] usager not found", {
sentry: true,
context: {
docUuid: request.params.docUuid,
structureId: user.structureId,
user: user.id,
role: user.role,
},
});
throw new HttpException("USAGER_DOC_NOT_FOUND", HttpStatus.BAD_REQUEST);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { UsagerDoc } from "../../../_common/model/usager/UsagerDoc.type";
import { Column, Entity, Index, JoinColumn, ManyToOne } from "typeorm";

import { AppTypeormTable } from "../_core/AppTypeormTable.typeorm";
import { StructureTable } from "../structure";
import { UsagerTable } from "./UsagerTable.typeorm";
import { UsagerDoc } from "@domifa/common";

// https://typeorm.io/#/entities/column-types-for-postgres
@Entity({ name: "usager_docs" })
export class UsagerDocsTable
extends AppTypeormTable<UsagerDocsTable>
Expand Down Expand Up @@ -48,6 +46,9 @@ export class UsagerDocsTable
@Column({ type: "integer", nullable: true })
public encryptionVersion: number;

@Column({ type: "boolean", default: false })
public shared: boolean;

public constructor(entity?: Partial<UsagerDocsTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { myDataSource } from "./../_postgres/appTypeormManager.service";
import { UsagerDoc } from "./../../../_common/model/usager/UsagerDoc.type";
import { UsagerDocsTable } from "./../../entities/usager/UsagerDocsTable.typeorm";
import { UsagerDoc } from "@domifa/common";
import { UsagerDocsTable } from "../../entities";
import { myDataSource } from "../_postgres";

export const USAGER_DOCS_FIELDS_TO_SELECT = {
filetype: true,
label: true,
uuid: true,
createdAt: true,
createdBy: true,
shared: true,
};

export const usagerDocsRepository = myDataSource
.getRepository<UsagerDoc>(UsagerDocsTable)
Expand All @@ -11,13 +20,7 @@ export const usagerDocsRepository = myDataSource
usagerRef,
structureId,
},
select: {
filetype: true,
label: true,
uuid: true,
createdAt: true,
createdBy: true,
},
select: USAGER_DOCS_FIELDS_TO_SELECT,
order: {
createdAt: "DESC",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ export class StructureDocController {
customDocType: structureDocDto.customDocType,
});

// Ajout du document
try {
await structureDocRepository.insert(newDoc);

const docs = await structureDocRepository.findBy({
structureId: user.structureId,
});
Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/structures/dto/structure-doc.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
IsNotEmpty,
IsString,
MaxLength,
MinLength,
ValidateIf,
} from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { StructureCustomDocType } from "@domifa/common";
import { StripTagsTransform } from "../../_common/decorators";

export class StructureDocDto {
@ApiProperty({
Expand All @@ -17,7 +19,9 @@ export class StructureDocDto {
@ValidateIf((o) => o.custom === true)
@IsNotEmpty()
@IsString()
@MaxLength(200)
@MinLength(2)
@MaxLength(100)
@StripTagsTransform()
public label: string;

@ApiProperty({
Expand Down
Loading

0 comments on commit 53231a8

Please sign in to comment.