Skip to content

Commit

Permalink
eslint rule to forbid nested ternaries
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Jul 4, 2023
1 parent 6b9fddd commit 421ca75
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .tooling/.eslint/eslint.rules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
"arrow-body-style": ["error", "as-needed"],
"object-shorthand": ["error", "always"],
"no-nested-ternary": "error",
"no-return-await": "off",
"require-await": "off",
"no-console": "error",
Expand Down Expand Up @@ -201,7 +202,6 @@ const toAdd = {
"no-multi-assign": "error",
"no-multi-str": "error",
"no-negated-condition": "error",
"no-nested-ternary": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-object": "error",
Expand Down
62 changes: 44 additions & 18 deletions back/src/adapters/secondary/pg/PgConventionRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,20 @@ export class PgConventionRepository implements ConventionRepository {
id,
establishmentTutor,
);
const beneficiaryCurrentEmployerId = beneficiaryCurrentEmployer
? (await this.getBeneficiaryCurrentEmployerId(id)) === null
? await this.insertBeneficiaryCurrentEmployer(
beneficiaryCurrentEmployer,
)
: await this.updateBeneficiaryCurrentEmployer(
id,
beneficiaryCurrentEmployer,
)
: null;

const beneficiaryCurrentEmployerId =
await this.insertOrUpdateBeneficiaryCurrentEmployerIfExists(
beneficiaryCurrentEmployer,
id,
);

await this.updateBeneficiary(id, beneficiary);

const beneficiaryRepresentativeId = beneficiaryRepresentative
? (await this.getBeneficiaryRepresentativeId(id)) === null
? await this.insertBeneficiaryRepresentative(beneficiaryRepresentative)
: await this.updateBeneficiaryRepresentative(
id,
beneficiaryRepresentative,
)
: null;
const beneficiaryRepresentativeId =
await this.insertOrUpdateBeneficiaryRepresentativeIfExists(
beneficiaryRepresentative,
id,
);

await this.updateConvention({
convention,
Expand Down Expand Up @@ -446,6 +439,39 @@ export class PgConventionRepository implements ConventionRepository {
if (result) return result.id;
throw new Error(missingReturningRowError(updateReturn));
}

private async insertOrUpdateBeneficiaryCurrentEmployerIfExists(
beneficiaryCurrentEmployer: BeneficiaryCurrentEmployer | undefined,
conventionId: ConventionId,
): Promise<number | null> {
if (!beneficiaryCurrentEmployer) return null;

const beneficiaryCurrentEmployerIdInDb =
await this.getBeneficiaryCurrentEmployerId(conventionId);

return beneficiaryCurrentEmployerIdInDb === null
? this.insertBeneficiaryCurrentEmployer(beneficiaryCurrentEmployer)
: this.updateBeneficiaryCurrentEmployer(
conventionId,
beneficiaryCurrentEmployer,
);
}
private async insertOrUpdateBeneficiaryRepresentativeIfExists(
beneficiaryRepresentative: BeneficiaryRepresentative | undefined,
conventionId: ConventionId,
): Promise<number | null> {
if (!beneficiaryRepresentative) return null;

const beneficiaryRepresentativeId =
await this.getBeneficiaryRepresentativeId(conventionId);

return beneficiaryRepresentativeId === null
? this.insertBeneficiaryRepresentative(beneficiaryRepresentative)
: this.updateBeneficiaryRepresentative(
conventionId,
beneficiaryRepresentative,
);
}
}
const missingReturningRowError = (updateReturn: QueryResult<any>): string =>
`Missing rows on update return: ${JSON.stringify(updateReturn.rows)}`;
26 changes: 10 additions & 16 deletions front/src/app/components/admin/ConventionManageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,18 @@ export const ConventionManageContent = ({
.push();
}

if (!isLoading) return <Loader />;
if (!convention) return <p>Pas de conventions correspondante trouvée</p>;
return (
<>
{isLoading ? (
<Loader />
) : !convention ? (
<p>Pas de conventions correspondante trouvée</p>
) : (
<>
<ConventionValidation convention={convention} />
<ConventionManageActions
jwt={jwt}
convention={convention}
role={role}
submitFeedback={submitFeedback}
/>
<NpsSection convention={convention} role={role} />
</>
)}
<ConventionValidation convention={convention} />
<ConventionManageActions
jwt={jwt}
convention={convention}
role={role}
submitFeedback={submitFeedback}
/>
<NpsSection convention={convention} role={role} />
</>
);
};
11 changes: 7 additions & 4 deletions shared/src/utils/toDotNotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ export const toDotNotation = (input: object, parentKey?: string) =>
Object.keys(input || {}).reduce((acc, key): object => {
const value = input[key as keyof typeof input];
const outputKey = parentKey ? `${parentKey}.${key}` : `${key}`;
return value && typeof value === "object"
? Object.keys(value).length > 0

if (value && typeof value === "object") {
return Object.keys(value).length > 0
? { ...acc, ...toDotNotation(value, outputKey) }
: acc
: { ...acc, [outputKey]: value };
: acc;
}

return { ...acc, [outputKey]: value };
}, {});

0 comments on commit 421ca75

Please sign in to comment.