Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add note to secret and variable #151

Merged
merged 2 commits into from
Feb 22, 2024
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
2 changes: 2 additions & 0 deletions apps/api/src/event/event.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ describe('Event Controller Tests', () => {
{
name: 'My secret',
value: 'My value',
note: 'Some note',
environmentId: environment.id,
rotateAfter: '720'
},
Expand Down Expand Up @@ -305,6 +306,7 @@ describe('Event Controller Tests', () => {
{
name: 'My variable',
value: 'My value',
note: 'Some note',
environmentId: environment.id
},
project.id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Secret" ADD COLUMN "note" TEXT;

-- AlterTable
ALTER TABLE "Variable" ADD COLUMN "note" TEXT;
2 changes: 2 additions & 0 deletions apps/api/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ model Secret {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
rotateAt DateTime?
note String?

lastUpdatedBy User? @relation(fields: [lastUpdatedById], references: [id], onUpdate: Cascade, onDelete: SetNull)
lastUpdatedById String?
Expand Down Expand Up @@ -355,6 +356,7 @@ model Variable {
versions VariableVersion[] // Stores the versions of the variable
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
note String?

lastUpdatedBy User? @relation(fields: [lastUpdatedById], references: [id], onUpdate: Cascade, onDelete: SetNull)
lastUpdatedById String?
Expand Down
7 changes: 6 additions & 1 deletion apps/api/src/secret/dto/create.secret/create.secret.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNumber, IsOptional, IsString } from 'class-validator'
import { IsNumber, IsOptional, IsString, Length } from 'class-validator'

export class CreateSecret {
@IsString()
Expand All @@ -7,6 +7,11 @@ export class CreateSecret {
@IsString()
value: string

@IsString()
@IsOptional()
@Length(0, 100)
note: string

@IsNumber()
@IsOptional()
environmentId: string
Expand Down
11 changes: 8 additions & 3 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ describe('Secret Controller Tests', () => {
payload: {
environmentId: environment2.id,
name: 'Secret 1',
note: 'Secret 1 note',
value: 'Secret 1 value',
rotateAfter: '24'
},
Expand All @@ -213,6 +214,7 @@ describe('Secret Controller Tests', () => {

expect(body).toBeDefined()
expect(body.name).toBe('Secret 1')
expect(body.note).toBe('Secret 1 note')
expect(body.environmentId).toBe(environment2.id)
expect(body.projectId).toBe(project1.id)

Expand Down Expand Up @@ -417,12 +419,13 @@ describe('Secret Controller Tests', () => {
)
})

it('should be able to update the environment name without creating a new version', async () => {
it('should be able to update the secret name and note without creating a new version', async () => {
const response = await app.inject({
method: 'PUT',
url: `/secret/${secret1.id}`,
payload: {
name: 'Updated Secret 1'
name: 'Updated Secret 1',
note: 'Updated Secret 1 note'
},
headers: {
'x-e2e-user-email': user1.email
Expand All @@ -431,6 +434,7 @@ describe('Secret Controller Tests', () => {

expect(response.statusCode).toBe(200)
expect(response.json().name).toEqual('Updated Secret 1')
expect(response.json().note).toEqual('Updated Secret 1 note')

const secretVersion = await prisma.secretVersion.findMany({
where: {
Expand Down Expand Up @@ -764,7 +768,8 @@ describe('Secret Controller Tests', () => {
environmentId: environment1.id,
name: 'Secret 20',
value: 'Secret 20 value',
rotateAfter: '24'
rotateAfter: '24',
note: 'Secret 20 note'
},
project2.id
)
Expand Down
11 changes: 8 additions & 3 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import getEnvironmentWithAuthority from '../../common/get-environment-with-autho
import getSecretWithAuthority from '../../common/get-secret-with-authority'
import { SecretWithVersion } from '../secret.types'
import createEvent from '../../common/create-event'
import getDefaultEnvironmentOfProject from '../../common/get-default-project-environemnt'
import getDefaultEnvironmentOfProject from '../../common/get-default-project-environment'

@Injectable()
export class SecretService {
Expand Down Expand Up @@ -76,6 +76,7 @@ export class SecretService {
const secret = await this.prisma.secret.create({
data: {
name: dto.name,
note: dto.note,
rotateAt: addHoursToDate(dto.rotateAfter),
versions: {
create: {
Expand Down Expand Up @@ -169,6 +170,7 @@ export class SecretService {
},
data: {
name: dto.name,
note: dto.note,
rotateAt: addHoursToDate(dto.rotateAfter),
lastUpdatedById: user.id,
versions: {
Expand All @@ -186,8 +188,11 @@ export class SecretService {
id: secretId
},
data: {
name: dto.name ?? secret.name,
rotateAt: dto.rotateAfter ?? secret.rotateAt,
note: dto.note,
name: dto.name,
rotateAt: dto.rotateAfter
? addHoursToDate(dto.rotateAfter)
: undefined,
lastUpdatedById: user.id
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNumber, IsOptional, IsString } from 'class-validator'
import { IsNumber, IsOptional, IsString, Length } from 'class-validator'

export class CreateVariable {
@IsString()
Expand All @@ -7,6 +7,11 @@ export class CreateVariable {
@IsString()
value: string

@IsString()
@IsOptional()
@Length(0, 100)
note: string

@IsNumber()
@IsOptional()
environmentId: string
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { CreateVariable } from '../dto/create.variable/create.variable'
import getProjectWithAuthority from '../../common/get-project-with-authority'
import getEnvironmentWithAuthority from '../../common/get-environment-with-authority'
import getDefaultEnvironmentOfProject from '../../common/get-default-project-environemnt'
import getDefaultEnvironmentOfProject from '../../common/get-default-project-environment'
import createEvent from '../../common/create-event'
import { UpdateVariable } from '../dto/update.variable/update.variable'
import getVariableWithAuthority from '../../common/get-variable-with-authority'
Expand Down Expand Up @@ -76,6 +76,7 @@ export class VariableService {
const variable = await this.prisma.variable.create({
data: {
name: dto.name,
note: dto.note,
versions: {
create: {
value: dto.value,
Expand Down Expand Up @@ -176,6 +177,7 @@ export class VariableService {
},
data: {
name: dto.name,
note: dto.note,
lastUpdatedById: user.id,
versions: {
create: {
Expand All @@ -192,7 +194,8 @@ export class VariableService {
id: variableId
},
data: {
name: dto.name ?? variable.name,
note: dto.note,
name: dto.name,
lastUpdatedById: user.id
}
})
Expand Down
10 changes: 8 additions & 2 deletions apps/api/src/variable/variable.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ describe('Variable Controller Tests', () => {
environmentId: environment2.id,
name: 'Variable 1',
value: 'Variable 1 value',
note: 'Variable 1 note',
rotateAfter: '24'
},
headers: {
Expand All @@ -213,6 +214,7 @@ describe('Variable Controller Tests', () => {

expect(body).toBeDefined()
expect(body.name).toBe('Variable 1')
expect(body.note).toBe('Variable 1 note')
expect(body.environmentId).toBe(environment2.id)
expect(body.projectId).toBe(project1.id)

Expand All @@ -238,6 +240,7 @@ describe('Variable Controller Tests', () => {
payload: {
name: 'Variable 2',
value: 'Variable 2 value',
note: 'Variable 2 note',
rotateAfter: '24'
},
headers: {
Expand All @@ -251,6 +254,7 @@ describe('Variable Controller Tests', () => {

expect(body).toBeDefined()
expect(body.name).toBe('Variable 2')
expect(body.note).toBe('Variable 2 note')
expect(body.environmentId).toBe(environment1.id)
expect(body.projectId).toBe(project1.id)
})
Expand Down Expand Up @@ -417,12 +421,13 @@ describe('Variable Controller Tests', () => {
)
})

it('should be able to update the environment name without creating a new version', async () => {
it('should be able to update the variable name and note without creating a new version', async () => {
const response = await app.inject({
method: 'PUT',
url: `/variable/${variable1.id}`,
payload: {
name: 'Updated Variable 1'
name: 'Updated Variable 1',
note: 'Updated Variable 1 note'
},
headers: {
'x-e2e-user-email': user1.email
Expand All @@ -431,6 +436,7 @@ describe('Variable Controller Tests', () => {

expect(response.statusCode).toBe(200)
expect(response.json().name).toEqual('Updated Variable 1')
expect(response.json().note).toEqual('Updated Variable 1 note')

const variableVersion = await prisma.variableVersion.findMany({
where: {
Expand Down
Loading