-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add variable module and relevant features
- Loading branch information
Showing
18 changed files
with
950 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Environment, PrismaClient, Project } from '@prisma/client' | ||
|
||
export default async function getDefaultEnvironmentOfProject( | ||
projectId: Project['id'], | ||
prisma: PrismaClient | ||
): Promise<Environment | null> { | ||
return await prisma.environment.findFirst({ | ||
where: { | ||
projectId, | ||
isDefault: true | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Authority, PrismaClient, User, Variable } from '@prisma/client' | ||
import getCollectiveProjectAuthorities from './get-collective-project-authorities' | ||
import { NotFoundException, UnauthorizedException } from '@nestjs/common' | ||
import { VariableWithProjectAndVersion } from '../variable/variable.types' | ||
|
||
export default async function getVariableWithAuthority( | ||
userId: User['id'], | ||
variableId: Variable['id'], | ||
authority: Authority, | ||
prisma: PrismaClient | ||
): Promise<VariableWithProjectAndVersion> { | ||
// Fetch the variable | ||
let variable: VariableWithProjectAndVersion | ||
|
||
try { | ||
variable = await prisma.variable.findUnique({ | ||
where: { | ||
id: variableId | ||
}, | ||
include: { | ||
versions: true, | ||
project: true, | ||
environment: { | ||
select: { | ||
id: true, | ||
name: true | ||
} | ||
} | ||
} | ||
}) | ||
} catch (error) { | ||
/* empty */ | ||
} | ||
|
||
if (!variable) { | ||
throw new NotFoundException(`Variable with id ${variableId} not found`) | ||
} | ||
|
||
// Check if the user has the project in their workspace role list | ||
const permittedAuthorities = await getCollectiveProjectAuthorities( | ||
userId, | ||
variable.project, | ||
prisma | ||
) | ||
|
||
// Check if the user has the required authorities | ||
if ( | ||
!permittedAuthorities.has(authority) && | ||
!permittedAuthorities.has(Authority.WORKSPACE_ADMIN) | ||
) { | ||
throw new UnauthorizedException( | ||
`User ${userId} does not have the required authorities` | ||
) | ||
} | ||
|
||
return variable | ||
} |
91 changes: 91 additions & 0 deletions
91
apps/api/src/prisma/migrations/20240219163241_add_variable/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
-- AlterEnum | ||
-- This migration adds more than one value to an enum. | ||
-- With PostgreSQL versions 11 and earlier, this is not possible | ||
-- in a single migration. This can be worked around by creating | ||
-- multiple migrations, each migration adding only one value to | ||
-- the enum. | ||
|
||
|
||
ALTER TYPE "Authority" ADD VALUE 'CREATE_VARIABLE'; | ||
ALTER TYPE "Authority" ADD VALUE 'READ_VARIABLE'; | ||
ALTER TYPE "Authority" ADD VALUE 'UPDATE_VARIABLE'; | ||
ALTER TYPE "Authority" ADD VALUE 'DELETE_VARIABLE'; | ||
|
||
-- AlterEnum | ||
ALTER TYPE "EventSource" ADD VALUE 'VARIABLE'; | ||
|
||
-- AlterEnum | ||
-- This migration adds more than one value to an enum. | ||
-- With PostgreSQL versions 11 and earlier, this is not possible | ||
-- in a single migration. This can be worked around by creating | ||
-- multiple migrations, each migration adding only one value to | ||
-- the enum. | ||
|
||
|
||
ALTER TYPE "EventType" ADD VALUE 'VARIABLE_UPDATED'; | ||
ALTER TYPE "EventType" ADD VALUE 'VARIABLE_DELETED'; | ||
ALTER TYPE "EventType" ADD VALUE 'VARIABLE_ADDED'; | ||
|
||
-- AlterEnum | ||
-- This migration adds more than one value to an enum. | ||
-- With PostgreSQL versions 11 and earlier, this is not possible | ||
-- in a single migration. This can be worked around by creating | ||
-- multiple migrations, each migration adding only one value to | ||
-- the enum. | ||
|
||
|
||
ALTER TYPE "NotificationType" ADD VALUE 'VARIABLE_UPDATED'; | ||
ALTER TYPE "NotificationType" ADD VALUE 'VARIABLE_DELETED'; | ||
ALTER TYPE "NotificationType" ADD VALUE 'VARIABLE_ADDED'; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Event" ADD COLUMN "sourceVariableId" TEXT; | ||
|
||
-- CreateTable | ||
CREATE TABLE "VariableVersion" ( | ||
"id" TEXT NOT NULL, | ||
"value" TEXT NOT NULL, | ||
"version" INTEGER NOT NULL DEFAULT 1, | ||
"variableId" TEXT NOT NULL, | ||
"createdOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"createdById" TEXT, | ||
|
||
CONSTRAINT "VariableVersion_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Variable" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"lastUpdatedById" TEXT, | ||
"projectId" TEXT NOT NULL, | ||
"environmentId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Variable_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "VariableVersion_variableId_version_key" ON "VariableVersion"("variableId", "version"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Variable_projectId_environmentId_name_key" ON "Variable"("projectId", "environmentId", "name"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Event" ADD CONSTRAINT "Event_sourceVariableId_fkey" FOREIGN KEY ("sourceVariableId") REFERENCES "Variable"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "VariableVersion" ADD CONSTRAINT "VariableVersion_variableId_fkey" FOREIGN KEY ("variableId") REFERENCES "Variable"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "VariableVersion" ADD CONSTRAINT "VariableVersion_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Variable" ADD CONSTRAINT "Variable_lastUpdatedById_fkey" FOREIGN KEY ("lastUpdatedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Variable" ADD CONSTRAINT "Variable_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Variable" ADD CONSTRAINT "Variable_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "Environment"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.