Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libs/prisma-service/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ const createPlatformUser = async (): Promise<void> => {

logger.log(platformUser);
} else {
// User already exists — still need to set platformUserId so downstream
// functions (createPlatformOrganization) have a valid UUID for createdBy/lastChangedBy
platformUserId = existPlatformAdminUser[0].id;
Comment on lines +190 to +192

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether PLATFORM_ADMIN_EMAIL is validated anywhere before the Prisma lookup.
fd seed.ts . | xargs -I{} sh -c '
  echo "== {} =="
  sed -n "150,220p" "{}"
  echo
  rg -n "PLATFORM_ADMIN_EMAIL|findMany\\(|findUnique\\(" "{}"
'

Repository: credebl/platform

Length of output: 3849


🌐 Web query:

Does Prisma Client ignore undefinedvalues inwherefilters, and isfindUnique({ where: { email } }) the recommended API for a unique email column?

💡 Result:

Prisma Client behavior regarding undefined values in where filters is as follows: When you pass undefined as a value in a where filter, Prisma Client effectively ignores that filter criteria, as if it had not been provided at all [1][2]. This can lead to unexpected behavior, such as returning all records when you intended to filter by none [3][4]. To mitigate this, you can enable the strictUndefinedChecks preview feature (available in version 5.20.0 and later), which causes explicit undefined values to throw a runtime error and requires the use of Prisma.skip to intentionally omit a field [1][5]. Regarding the recommended API for a unique email column, findUnique({ where: { email } }) is indeed the correct and recommended API, provided that the email field is marked as @unique in your Prisma schema [6][7]. If the field is not marked as @unique, Prisma Client will not allow you to use it in a findUnique query; in such cases, you would receive an error and would need to either mark the field as unique or use findFirst instead [8][9][10].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the Prisma user model and nearby seed logic to confirm whether email is unique
# and whether PLATFORM_ADMIN_EMAIL is handled elsewhere.
echo "== seed.ts excerpt =="
sed -n '160,215p' libs/prisma-service/prisma/seed.ts

echo
echo "== user model references =="
rg -n "model User|model user|`@unique`|email" libs/prisma-service/prisma -g '*.prisma'

Repository: credebl/platform

Length of output: 4876


Validate PLATFORM_ADMIN_EMAIL before reusing the first user record.

If this env var is missing, prisma.user.findMany({ where: { email: undefined } }) drops the filter and existPlatformAdminUser[0].id can come from an arbitrary user. Fail fast here and switch to findUnique since email is unique.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libs/prisma-service/prisma/seed.ts` around lines 190 - 192, `seed.ts`
currently reuses the first result from `prisma.user.findMany` without validating
`PLATFORM_ADMIN_EMAIL`, which can accidentally match any user when the env var
is missing. Update the `platformUserId` lookup logic to fail fast if
`PLATFORM_ADMIN_EMAIL` is unset, and switch the user lookup to `findUnique` in
the platform admin branch so the unique `email` field is used safely before
assigning `existPlatformAdminUser[0].id`.

logger.log('Already seeding in user');
}
} catch (error) {
Expand Down