Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- DropForeignKey
ALTER TABLE "DeviceMetrics" DROP CONSTRAINT "DeviceMetrics_userId_fkey";

-- AlterTable
ALTER TABLE "DeviceMetrics" ALTER COLUMN "did" DROP NOT NULL,
ALTER COLUMN "browserName" DROP NOT NULL,
ALTER COLUMN "osName" DROP NOT NULL,
ALTER COLUMN "deviceType" DROP NOT NULL,
ALTER COLUMN "deviceName" DROP NOT NULL,
ALTER COLUMN "sessionId" DROP NOT NULL,
ALTER COLUMN "createdAt" DROP NOT NULL,
ALTER COLUMN "language" DROP NOT NULL,
ALTER COLUMN "userId" DROP NOT NULL;

-- AddForeignKey
ALTER TABLE "DeviceMetrics" ADD CONSTRAINT "DeviceMetrics_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
28 changes: 14 additions & 14 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ model BiharKrishiMetrics {
@@index([createdAt])
}
model DeviceMetrics {
id String @id @default(uuid())
did String
sessionId String
userId String @db.Uuid
deviceType String
deviceName String
browserName String
osName String
id String @id @default(uuid())
did String?
sessionId String?
userId String? @db.Uuid
deviceType String?
deviceName String?
browserName String?
osName String?
latitude Float?
longitude Float?
longitude Float?
state String?
district String?
language String
createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [id])
}
language String?
createdAt DateTime? @default(now())

user User? @relation(fields: [userId], references: [id])
}
13 changes: 10 additions & 3 deletions src/telemetry/telemetryMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ export async function telemetryMiddleware(
sessionId,
};
logger.log(`DEVICE INFO: ${JSON.stringify(deviceInfo)}`);
const userExists = await prisma.user.findUnique({
where: { id: userId },
});
let userExists;
if (userId) {
userExists = await prisma.user.findUnique({
where: { id: userId },
});
} else {
// handle the case where userId is not present
console.warn("userId is undefined, skipping user lookup");
}

if (userExists) {
const { userId, ...deviceInfoWithoutUserId } = deviceInfo;
await prisma.deviceMetrics.create({
Expand Down