Skip to content

Commit b7f9fd0

Browse files
committed
fix(backend): fix add referrer in creation
1 parent 939be67 commit b7f9fd0

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

packages/backend/src/usagers/controllers/usagers.controller.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ import {
3434
USAGER_LIGHT_ATTRIBUTES,
3535
} from "../../database";
3636

37-
import { anonymizeFullName, cleanPath, getPhoneString } from "../../util";
37+
import {
38+
anonymizeFullName,
39+
appLogger,
40+
cleanPath,
41+
getPhoneString,
42+
} from "../../util";
3843
import {
3944
UserStructureAuthenticated,
4045
USER_STRUCTURE_ROLE_ALL,
@@ -77,11 +82,33 @@ export class UsagersController {
7782
) {}
7883

7984
@Post()
80-
public createUsager(
85+
public async createUsager(
8186
@Body() usagerDto: CreateUsagerDto,
82-
@CurrentUser() user: UserStructureAuthenticated
87+
@CurrentUser() currentUser: UserStructureAuthenticated,
88+
@Res() res: ExpressResponse
8389
) {
84-
return this.usagersService.create(usagerDto, user);
90+
try {
91+
// Check referrer
92+
if (usagerDto?.referrerId) {
93+
const user = await userStructureRepository.findOneBy({
94+
id: usagerDto.referrerId,
95+
structureId: currentUser.structureId,
96+
});
97+
98+
if (!user) {
99+
return res
100+
.status(HttpStatus.BAD_REQUEST)
101+
.json({ message: "CANNOT_FIND_REFERRER" });
102+
}
103+
}
104+
105+
return this.usagersService.create(usagerDto, currentUser);
106+
} catch (err) {
107+
appLogger.error(err);
108+
return res
109+
.status(HttpStatus.BAD_REQUEST)
110+
.json({ message: "CANNOT_CREATE_USAGER" });
111+
}
85112
}
86113

87114
@UseGuards(UsagerAccessGuard)
@@ -361,6 +388,7 @@ export class UsagersController {
361388
const buffer = await input(filePath).fillForm(pdfInfos).output();
362389
return res.setHeader("content-type", "application/pdf").send(buffer);
363390
} catch (err) {
391+
appLogger.error(err);
364392
return res
365393
.status(HttpStatus.INTERNAL_SERVER_ERROR)
366394
.json({ message: "CERFA_ERROR" });

packages/backend/src/usagers/services/usagersCreator.service.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ function setUsagerDefaultAttributes(usager: Partial<Usager>): void {
3131
};
3232
usager.typeDom = usager?.typeDom ?? "PREMIERE_DOM";
3333
usager.pinnedNote = null;
34-
usager.referrerId = null;
34+
usager.referrerId = usager?.referrerId ?? null;
3535

36-
if (!usager.ayantsDroits) {
36+
if (!usager?.ayantsDroits?.length) {
3737
usager.ayantsDroits = [];
3838
}
3939

40-
if (!usager.historique) {
40+
if (!usager?.historique?.length) {
4141
usager.historique = [];
4242
}
4343

@@ -52,17 +52,15 @@ function setUsagerDefaultAttributes(usager: Partial<Usager>): void {
5252
};
5353
}
5454

55-
if (!usager.langue || usager.langue === "") {
55+
if (!usager?.langue || usager.langue === "") {
5656
usager.langue = null;
5757
}
5858

59-
if (!usager.lastInteraction) {
60-
usager.lastInteraction = {
61-
dateInteraction: new Date(),
62-
colisIn: 0,
63-
courrierIn: 0,
64-
recommandeIn: 0,
65-
enAttente: false,
66-
};
67-
}
59+
usager.lastInteraction = {
60+
dateInteraction: new Date(),
61+
colisIn: 0,
62+
courrierIn: 0,
63+
recommandeIn: 0,
64+
enAttente: false,
65+
};
6866
}

0 commit comments

Comments
 (0)