Skip to content
Merged
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
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@trpc/client": "^11.6.0",
"@trpc/server": "^11.6.0",
"@trpc/tanstack-react-query": "^11.6.0",
"@types/semver": "^7.7.1",
"@xyflow/react": "^12.8.6",
"ai": "5.0.100",
"better-auth": "1.4.9",
Expand Down Expand Up @@ -122,6 +121,7 @@
"@types/nodemailer": "^6.4.21",
"@types/react": "^19",
"@types/react-dom": "^19",
"@types/semver": "^7.7.1",
"@types/supertest": "^6.0.3",
"@types/toposort": "^2.0.7",
"@types/turndown": "^5.0.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Warnings:

- You are about to drop the column `deviceGroupId` on the `notification_device_group_mapping` table. All the data in the column will be lost.
- A unique constraint covering the columns `[notificationId,deviceGroupMatchingId]` on the table `notification_device_group_mapping` will be added. If there are existing duplicate values, this will fail.
- Added the required column `deviceGroupMatchingId` to the `notification_device_group_mapping` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE "notification_device_group_mapping" DROP CONSTRAINT "notification_device_group_mapping_deviceGroupId_fkey";

-- DropIndex
DROP INDEX "notification_device_group_mapping_notificationId_deviceGrou_key";

-- AlterTable
ALTER TABLE "notification_device_group_mapping" DROP COLUMN "deviceGroupId",
ADD COLUMN "deviceGroupMatchingId" TEXT NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "notification_device_group_mapping_notificationId_deviceGrou_key" ON "notification_device_group_mapping"("notificationId", "deviceGroupMatchingId");

-- AddForeignKey
ALTER TABLE "notification_device_group_mapping" ADD CONSTRAINT "notification_device_group_mapping_deviceGroupMatchingId_fkey" FOREIGN KEY ("deviceGroupMatchingId") REFERENCES "device_group_matching"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[workOrderTicketId,deviceGroupMatchingId]` on the table `notification_device_group_mapping` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "ndg_mapping_workorder_devicegroupmatching_key" ON "notification_device_group_mapping"("workOrderTicketId", "deviceGroupMatchingId");
23 changes: 9 additions & 14 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,7 @@ model DeviceGroup {
updatedAt DateTime @updatedAt
deviceGroupHistories DeviceGroupHistory[]
assets Asset[]
// Vulnerabilities / remediations / advisories / device artifacts connect via
// DeviceGroupMatching now, not a direct relation. notificationMappings is from
// main's notifications feature and stays.
notificationMappings NotificationDeviceGroupMapping[]

// NOTE: Postgres treats NULLs as distinct, so this composite unique will NOT
// prevent duplicate rows when versionId is null. Identity resolution is done
// in the app via resolveDeviceGroup() (findFirst + create in a transaction).

@@unique([vendorId, productId, versionId, versionStatus], name: "device_group_identity")
@@index([vendorId, productId])
@@map("device_group")
Expand All @@ -310,6 +303,7 @@ model DeviceGroupMatching {
remediations Remediation[] @relation("RemediationMatchings")
advisories Advisory[] @relation("AdvisoryMatchings")
deviceArtifacts DeviceArtifact[] @relation("DeviceArtifactMatchings")
notificationMappings NotificationDeviceGroupMapping[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -1163,8 +1157,8 @@ model Notification {

sources NotificationSource[]
reads NotificationRead[]
assets NotificationAssetMapping[]
deviceGroups NotificationDeviceGroupMapping[]
assets NotificationAssetMapping[]
deviceGroupsMatchings NotificationDeviceGroupMapping[]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider renaming to deviceGroupMatchings, here (not deviceGroup_s_Matchings)

And see the relevant coderabbit error here: https://github.com/PATCH-UPGRADE/viper/pull/147/changes#r3514907510

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

vulnerabilities NotificationVulnerabilityMapping[]
remediations NotificationRemediationMapping[]

Expand Down Expand Up @@ -1251,14 +1245,15 @@ model NotificationDeviceGroupMapping {
notification Notification? @relation(fields: [notificationId], references: [id], onDelete: Cascade)
workOrderTicketId String?
workOrderTicket WorkOrderTicket? @relation(fields: [workOrderTicketId], references: [id], onDelete: Cascade)
deviceGroupId String
deviceGroup DeviceGroup @relation(fields: [deviceGroupId], references: [id], onDelete: Cascade)
deviceGroupMatchingId String
deviceGroupMatching DeviceGroupMatching @relation(fields:[deviceGroupMatchingId], references: [id], onDelete: Cascade)
confidence ConfidenceLevel?
reasonWhy String?

@@unique([notificationId, deviceGroupId])
@@unique([workOrderTicketId, deviceGroupId], map: "ndg_mapping_workorder_devicegroup_key")
@@unique([notificationId, deviceGroupMatchingId])
@@unique([workOrderTicketId, deviceGroupMatchingId], map: "ndg_mapping_workorder_devicegroupmatching_key")
@@index([workOrderTicketId])

@@map("notification_device_group_mapping")
}

Expand Down
17 changes: 13 additions & 4 deletions scripts/seed-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ async function seedDeviceGroups(userId: string) {
const existing = await prisma.deviceGroup.findFirst({ where: identity });
const deviceGroup =
existing ?? (await prisma.deviceGroup.create({ data: identity }));

const existingMatching = await prisma.deviceGroupMatching.findFirst({
where: { vendorId: vendor.id, productId: product.id, versionId: null },
});
const deviceGroupMatching =
existingMatching ??
(await prisma.deviceGroupMatching.create({ data: identity }));
const existingAssets = await prisma.asset.count({
where: { deviceGroupId: deviceGroup.id },
});
Expand All @@ -104,7 +109,11 @@ async function seedDeviceGroups(userId: string) {
console.log(
` ✅ ${dg.vendor} ${dg.product} (${deviceGroup.id}) — ${dg.assetCount} assets`,
);
results.push({ ...dg, deviceGroupId: deviceGroup.id });
results.push({
...dg,
deviceGroupId: deviceGroup.id,
deviceGroupMatchingId: deviceGroupMatching.id,
});
}

return results;
Expand Down Expand Up @@ -355,7 +364,7 @@ async function getSeedUser() {

async function seedNotifications(
_userId: string,
deviceGroups: Array<{ deviceGroupId: string }>,
deviceGroups: Array<{ deviceGroupMatchingId: string }>,
) {
console.log("\n🌱 Seeding notifications...");

Expand Down Expand Up @@ -407,7 +416,7 @@ async function seedNotifications(
await prisma.notificationDeviceGroupMapping.create({
data: {
notificationId: notification.id,
deviceGroupId: dg.deviceGroupId,
deviceGroupMatchingId: dg.deviceGroupMatchingId,
confidence:
Math.random() > 0.3
? ConfidenceLevel.Confirmed
Expand Down
Loading
Loading