From 8bbe2583cb7d74e1696174006a62c588f991a188 Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Tue, 30 Jan 2024 15:48:50 +0530 Subject: [PATCH] Add `createdBy` column in deployments table (#51) * Add and use createdBy field in deployment entity * Use updated get deployments client method in UI --------- Co-authored-by: neeraj --- packages/backend/src/database.ts | 20 +++++++++++++------ packages/backend/src/entity/Deployment.ts | 5 +++++ packages/backend/src/resolvers.ts | 4 ++-- packages/backend/src/schema.gql | 1 + packages/backend/src/utils.ts | 1 + .../backend/test/fixtures/deployments.json | 10 ++++++++++ packages/backend/test/initialize-db.ts | 3 ++- .../deployments/DeploymentDetailsCard.tsx | 2 +- packages/gql-client/src/queries.ts | 5 +++++ packages/gql-client/src/types.ts | 17 ++++++++-------- 10 files changed, 50 insertions(+), 18 deletions(-) diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 88e7174d..dbf7060d 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -28,12 +28,12 @@ export class Database { }); } - async init () : Promise { + async init (): Promise { await this.dataSource.initialize(); log('database initialized'); } - async getUser (userId: number) : Promise { + async getUser (userId: number): Promise { const userRepository = this.dataSource.getRepository(User); const user = await userRepository.findOneBy({ id: userId @@ -42,7 +42,7 @@ export class Database { return user; } - async getOrganizationsByUserId (userId: number) : Promise { + async getOrganizationsByUserId (userId: number): Promise { const userOrganizationRepository = this.dataSource.getRepository(UserOrganization); const userOrgs = await userOrganizationRepository.find({ @@ -118,12 +118,16 @@ export class Database { const deployments = await deploymentRepository.find({ relations: { project: true, - domain: true + domain: true, + createdBy: true }, where: { project: { id: projectId } + }, + order: { + createdAt: 'DESC' } }); @@ -262,12 +266,13 @@ export class Database { } } - async redeployToProdById (deploymentId: string): Promise { + async redeployToProdById (userId: string, deploymentId: string): Promise { const deploymentRepository = this.dataSource.getRepository(Deployment); const deployment = await deploymentRepository.findOne({ relations: { project: true, - domain: true + domain: true, + createdBy: true }, where: { id: Number(deploymentId) @@ -282,6 +287,9 @@ export class Database { if (updatedDeployment.environment === Environment.Production) { // TODO: Put isCurrent field in project updatedDeployment.isCurrent = true; + updatedDeployment.createdBy = Object.assign(new User(), { + id: Number(userId) + }); } await deploymentRepository.update({ id: Number(deploymentId) }, { domain: null, isCurrent: false }); diff --git a/packages/backend/src/entity/Deployment.ts b/packages/backend/src/entity/Deployment.ts index 1d2e20c2..14710b0f 100644 --- a/packages/backend/src/entity/Deployment.ts +++ b/packages/backend/src/entity/Deployment.ts @@ -11,6 +11,7 @@ import { import { Project } from './Project'; import { Domain } from './Domain'; +import { User } from './User'; export enum Environment { Production = 'Production', @@ -59,6 +60,10 @@ export class Deployment { }) status!: Status; + @ManyToOne(() => User) + @JoinColumn({ name: 'createdBy' }) + createdBy!: User; + @CreateDateColumn() createdAt!: Date; diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index 59e7be0e..928e27f1 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -162,9 +162,9 @@ export const createResolvers = async (db: Database): Promise => { } }, - redeployToProd: async (_: any, { deploymentId }: { deploymentId: string }) => { + redeployToProd: async (_: any, { deploymentId }: { deploymentId: string }, context: any) => { try { - await db.redeployToProdById(deploymentId); + await db.redeployToProdById(context.userId, deploymentId); return true; } catch (err) { log(err); diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index 74a74baa..729ba2e5 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -91,6 +91,7 @@ type Deployment { status: DeploymentStatus! createdAt: String! updatedAt: String! + createdBy: User! } type Domain { diff --git a/packages/backend/src/utils.ts b/packages/backend/src/utils.ts index 8fcf9a0d..32e48175 100644 --- a/packages/backend/src/utils.ts +++ b/packages/backend/src/utils.ts @@ -55,6 +55,7 @@ export const deploymentToGqlType = (dbDeployment: Deployment): any => { environment: dbDeployment.environment, isCurrent: dbDeployment.isCurrent, status: dbDeployment.status, + createdBy: dbDeployment.createdBy, createdAt: dbDeployment.createdAt, updatedAt: dbDeployment.updatedAt }; diff --git a/packages/backend/test/fixtures/deployments.json b/packages/backend/test/fixtures/deployments.json index 494b1e65..e4347bf7 100644 --- a/packages/backend/test/fixtures/deployments.json +++ b/packages/backend/test/fixtures/deployments.json @@ -2,6 +2,7 @@ { "projectIndex": 0, "domainIndex":0, + "createdByIndex": 0, "title": "nextjs-boilerplate-1", "status": "Building", "environment": "Production", @@ -12,6 +13,7 @@ { "projectIndex": 0, "domainIndex":1, + "createdByIndex": 0, "title": "nextjs-boilerplate-2", "status": "Ready", "environment": "Preview", @@ -22,6 +24,7 @@ { "projectIndex": 0, "domainIndex":2, + "createdByIndex": 0, "title": "nextjs-boilerplate-3", "status": "Error", "environment": "Development", @@ -32,6 +35,7 @@ { "projectIndex": 0, "domainIndex": null, + "createdByIndex": 0, "title": "nextjs-boilerplate-4", "status": "Ready", "environment": "Production", @@ -42,6 +46,7 @@ { "projectIndex": 1, "domainIndex":3, + "createdByIndex": 1, "title": "nextjs-boilerplate-1", "status": "Building", "environment": "Production", @@ -52,6 +57,7 @@ { "projectIndex": 1, "domainIndex":4, + "createdByIndex": 1, "title": "nextjs-boilerplate-2", "status": "Ready", "environment": "Preview", @@ -62,6 +68,7 @@ { "projectIndex": 1, "domainIndex":5, + "createdByIndex": 1, "title": "nextjs-boilerplate-3", "status": "Error", "environment": "Development", @@ -72,6 +79,7 @@ { "projectIndex": 2, "domainIndex":6, + "createdByIndex": 2, "title": "nextjs-boilerplate-1", "status": "Building", "environment": "Production", @@ -82,6 +90,7 @@ { "projectIndex": 2, "domainIndex":7, + "createdByIndex": 2, "title": "nextjs-boilerplate-2", "status": "Ready", "environment": "Preview", @@ -92,6 +101,7 @@ { "projectIndex": 2, "domainIndex":8, + "createdByIndex": 2, "title": "nextjs-boilerplate-3", "status": "Error", "environment": "Development", diff --git a/packages/backend/test/initialize-db.ts b/packages/backend/test/initialize-db.ts index 2c3e075a..69f61fb9 100644 --- a/packages/backend/test/initialize-db.ts +++ b/packages/backend/test/initialize-db.ts @@ -86,7 +86,8 @@ const generateTestData = async (dataSource: DataSource) => { const deploymentRelations = { project: savedProjects, - domain: savedDomains + domain: savedDomains, + createdBy: savedUsers }; await loadAndSaveData(Deployment, dataSource, path.resolve(__dirname, DEPLOYMENT_DATA_PATH), deploymentRelations); diff --git a/packages/frontend/src/components/projects/project/deployments/DeploymentDetailsCard.tsx b/packages/frontend/src/components/projects/project/deployments/DeploymentDetailsCard.tsx index 09d8f0b4..5a42ae13 100644 --- a/packages/frontend/src/components/projects/project/deployments/DeploymentDetailsCard.tsx +++ b/packages/frontend/src/components/projects/project/deployments/DeploymentDetailsCard.tsx @@ -102,7 +102,7 @@ const DeploymentDetailsCard = ({
- {relativeTimeMs(deployment.createdAt)} ^ {deployment.author} + {relativeTimeMs(deployment.createdAt)} ^ {deployment.createdBy.name} diff --git a/packages/gql-client/src/queries.ts b/packages/gql-client/src/queries.ts index 21590fa5..7c8864b9 100644 --- a/packages/gql-client/src/queries.ts +++ b/packages/gql-client/src/queries.ts @@ -158,6 +158,11 @@ query ($projectId: String!) { status createdAt updatedAt + createdBy { + id + name + email + } } } `; diff --git a/packages/gql-client/src/types.ts b/packages/gql-client/src/types.ts index 9bf85462..aa13d955 100644 --- a/packages/gql-client/src/types.ts +++ b/packages/gql-client/src/types.ts @@ -47,6 +47,14 @@ export type Domain = { updatedAt: string } +export type User = { + id: string + name: string + email: string + createdAt: string + updatedAt: string +} + export type Deployment = { id: string domain: Domain @@ -56,14 +64,7 @@ export type Deployment = { environment: Environment isCurrent: boolean status: DeploymentStatus - createdAt: string - updatedAt: string -} - -export type User = { - id: string - name: string - email: string + createdBy: User createdAt: string updatedAt: string }