Skip to content

Commit

Permalink
feat(backend): add phone in contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Dec 30, 2024
1 parent b481cec commit e4b4b40
Show file tree
Hide file tree
Showing 40 changed files with 279 additions and 157 deletions.
Binary file modified _scripts/db/dumps/domifa_test.postgres.custom.gz
Binary file not shown.
42 changes: 7 additions & 35 deletions _scripts/db/dumps/domifa_test.postgres.restore-data-only.sql

Large diffs are not rendered by default.

Large diffs are not rendered by default.

27 changes: 11 additions & 16 deletions packages/backend/src/_common/decorators/IsValidPhoneDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ValidationOptions,
registerDecorator,
ValidationArguments,
} from "class-validator";
import { ValidationOptions, registerDecorator } from "class-validator";
import { isAnyValidPhone, isValidMobilePhone } from "../../util/phone";

export function IsValidPhone(
Expand All @@ -20,23 +16,22 @@ export function IsValidPhone(
options: validationOptions,
constraints: [property],
validator: {
validate(value: any, args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
const relatedValue = (args.object as any)[relatedPropertyName];

if (!value && !relatedValue) {
return false;
validate(value: any) {
if (typeof value === "string") {
try {
value = JSON.parse(value);
} catch (e) {
return false;
}
}

if (!value?.numero && !required) {
return true;
if (!value?.numero && required) {
return false;
}

if (
typeof value.numero !== "string" ||
typeof relatedValue.numero !== "string" ||
typeof value.countryCode !== "string" ||
typeof relatedValue.countryCode !== "string"
typeof value.countryCode !== "string"
) {
return !required;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export type ContactSupport = AppEntity & {
subject?: string;
email: string;
name: string; // Nom de l'interlocuteur
phone?: string;
structureName: string; // Nom de la structure
};
46 changes: 46 additions & 0 deletions packages/backend/src/_migrations/1735574440877-auto-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { domifaConfig } from "../config";

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

public async up(queryRunner: QueryRunner): Promise<void> {
if (
domifaConfig().envId === "prod" ||
domifaConfig().envId === "preprod" ||
domifaConfig().envId === "local"
) {
await queryRunner.query(
`DROP INDEX "public"."IDX_57133463f2311234ecf27157fa"`
);
await queryRunner.query(
`ALTER TABLE "structure" DROP COLUMN "filesUpdated"`
);
await queryRunner.query(`ALTER TABLE "contact_support" ADD "phone" text`);
await queryRunner.query(
`ALTER TABLE "usager" ALTER COLUMN "nom_prenom_surnom_ref" SET NOT NULL`
);
await queryRunner.query(
`CREATE INDEX "IDX_f072e2874bd87ecb6da2fbd66e" ON "usager" ("nom_prenom_surnom_ref") `
);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX "public"."IDX_f072e2874bd87ecb6da2fbd66e"`
);
await queryRunner.query(
`ALTER TABLE "usager" ALTER COLUMN "nom_prenom_surnom_ref" DROP NOT NULL`
);
await queryRunner.query(
`ALTER TABLE "contact_support" DROP COLUMN "phone"`
);
await queryRunner.query(
`ALTER TABLE "structure" ADD "filesUpdated" boolean NOT NULL DEFAULT false`
);
await queryRunner.query(
`CREATE INDEX "IDX_57133463f2311234ecf27157fa" ON "usager" ("nom_prenom_surnom_ref") `
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async function createTables(queryRunner: QueryRunner) {
"name" text NOT NULL,
"structureName" text NULL,
subject text NULL,
phone text NULL,
CONSTRAINT "PK_8e4a4781a01061a482fa33e5f5a" PRIMARY KEY (uuid)
);
Expand Down Expand Up @@ -430,14 +431,14 @@ async function createTables(queryRunner: QueryRunner) {
"pinnedNote" jsonb NULL,
nationalite text NULL,
statut text DEFAULT 'INSTRUCTION'::text NOT NULL,
nom_prenom_surnom_ref varchar NULL,
nom_prenom_surnom_ref varchar NOT NULL,
CONSTRAINT "PK_1bb36e24229bec446a281573612" PRIMARY KEY (uuid),
CONSTRAINT "UQ_e76056fb098740de66d58a5055a" UNIQUE ("structureId", ref),
CONSTRAINT "FK_a44d882d224e368efdee8eb8c80" FOREIGN KEY ("structureId") REFERENCES public."structure"(id) ON DELETE CASCADE
);
CREATE INDEX "IDX_57133463f2311234ecf27157fa" ON public.usager USING btree (nom_prenom_surnom_ref);
CREATE INDEX "IDX_a44d882d224e368efdee8eb8c8" ON public.usager USING btree ("structureId");
CREATE INDEX "IDX_b4d09870ec6cad2d2d98b7cc3a" ON public.usager USING btree (migrated);
CREATE INDEX "IDX_f072e2874bd87ecb6da2fbd66e" ON public.usager USING btree (nom_prenom_surnom_ref);
CREATE INDEX idx_usager_statut ON public.usager USING btree ("structureId", statut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Domifa</title>

<style>
@media only screen and (max-width: 620px) {
table[class="body"] h1 {
Expand Down Expand Up @@ -115,9 +115,9 @@
}
}
</style>



</head>
<body class style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f6f6f6; width: 100%;" width="100%" bgcolor="#f6f6f6">
Expand All @@ -133,7 +133,7 @@
<tr>
<td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">

<!-- DOMIFA LOGO -->
<!-- DOMIFA LOGO -->
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; border-collapse: collapse; border-spacing: 0px;" width="100%">
<tbody>
<tr>
Expand All @@ -146,7 +146,7 @@
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="my-8" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; margin-top: 32px; margin-bottom: 32px;" width="100%">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">


<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">
<b>5 erreurs</b> ont été rencontrées lors des traitements sur l'environnement <b>"test"</b>
Expand All @@ -168,7 +168,7 @@
</p>






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<strong>Email : </strong> {{ email }}
</p>

<p>
<strong>Téléphone : </strong> {% if phone %}{{ phone }}{% else %}non renseigné{% endif %}
</p>

<p>
<strong>Contenu du message: </strong><br><br> {{ content }}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ <h2 style="color: #000000; font-family: sans-serif; font-weight: 400; line-heigh
<strong>Email : </strong> [email protected]
</p>


<p
style="
font-family: sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 15px;
"
>
<strong>Téléphone : </strong> non renseigné
</p>

<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">
<strong>Contenu du message: </strong><br><br> Do esse tempor veniam incididunt deserunt ad labore laboris commodo id exercitation sit. Sunt minim tempor ad quis esse est eiusmod incididunt id dolor adipisicing ex excepteur. Consectetur consectetur dolore tempor nisi enim pariatur. Nostrud ea reprehenderit minim pariatur irure. Velit anim qui veniam et in excepteur duis eiusmod est culpa voluptate enim eiusmod laboris. Ut sunt sint magna ullamco nostrud velit est pariatur sit ea consectetur commodo minim.
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Nom de la structure : {{ structureName }}

Email : {{ email }}

Téléphone : {% if phone %}{{ phone }}{% else %}non renseigné{% endif %}

Contenu du message: {{ content }}
2 changes: 0 additions & 2 deletions packages/backend/src/auth/guards/structure-access.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export class StructureAccessGuard implements CanActivate {
public async canActivate(context: ExecutionContext) {
const r = context.switchToHttp().getRequest();

console.log(r?.params?.structureId);
console.log(r?.user?.isSuperAdminDomifa);
if (!r?.params?.structureId || !r?.user?.isSuperAdminDomifa) {
appLogger.error("[StructureAccessGuard] invalid structureId for admin", {
sentry: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class ContactSupportTable
@Column({ type: "text", nullable: true })
public structureName: string;

@Column({ type: "text", nullable: true })
public phone: string;

public constructor(entity?: Partial<ContactSupportTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ExpressRequest, ExpressResponse } from "../../util/express";
import { FILES_SIZE_LIMIT } from "../../util/file-manager";
import { ContactSupportTable, contactSupportRepository } from "../../database";
import { contactSupportEmailSender } from "../mails/services/templates-renderers/contact-support";
import { getPhoneString } from "../../util";

@Controller("contact")
export class ContactSupportController {
Expand Down Expand Up @@ -56,7 +57,13 @@ export class ContactSupportController {
@UploadedFile() file: Express.Multer.File,
@Res() res: ExpressResponse
) {
const dataToSave = new ContactSupportTable(contactSupportDto);
const phone = contactSupportDto?.phone
? getPhoneString(
JSON.parse(contactSupportDto?.phone as unknown as string)
)
: null;

const dataToSave = new ContactSupportTable({ ...contactSupportDto, phone });

if (file) {
dataToSave.attachment = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
import { Transform, TransformFnParams } from "class-transformer";

import { MessageEmailAttachment } from "../../database/entities/message-email/MessageEmailAttachment.type";
import { LowerCaseTransform } from "../../_common/decorators";
import { IsValidPhone, LowerCaseTransform } from "../../_common/decorators";
import sanitizeHtml from "sanitize-html";
import { Telephone } from "@domifa/common";

export class ContactSupportDto {
@ApiProperty({
Expand Down Expand Up @@ -94,6 +95,20 @@ export class ContactSupportDto {
})
public subject!: string;

@IsValidPhone("phone", false, false)
public phone!: Telephone;

@IsEmpty()
public attachment!: MessageEmailAttachment;

@ApiProperty({
type: "string",
format: "binary",
required: false,
})
@IsOptional()
@Transform(({ value }: TransformFnParams) => {
return value instanceof File ? value : undefined;
})
file?: Express.Multer.File;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function sendMail(model: ContactSupport): Promise<void> {

const renderedTemplate = await contactSupportEmailRenderer.renderTemplate({
structureId: model.structureId,
phone: model?.phone,
content: model.content,
email: model.email,
name: model.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ export class UsagersController {
);

if (!isValid(parsedDate)) {
console.log({ searchString });

throw new BadRequestException(
'Format de date invalide. Utilisez le format "dd MM yyyy"'
);
Expand Down
9 changes: 1 addition & 8 deletions packages/backend/src/util/phone/phoneUtils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export const getPhoneString = (
telephone: Telephone,
excpectedFormat: PhoneNumberFormat = PhoneNumberFormat.INTERNATIONAL
): string => {
if (!telephone) {
return "";
}
if (!telephone?.numero || telephone?.numero === "") {
return "";
}
Expand All @@ -34,18 +31,14 @@ export const isValidMobilePhone = (value: Telephone): boolean | null => {
const numberType = phoneUtil.getNumberType(parsedValue);
return numberType === 1;
}
// Numéro invalide
return false;
} catch (e) {
return false;
}
};

export const isAnyValidPhone = (value: Telephone): boolean => {
if (!value) {
return false;
}
if (!value.numero || value.numero === "") {
if (!value?.numero || value?.numero === "") {
return false;
}
try {
Expand Down
Loading

0 comments on commit e4b4b40

Please sign in to comment.