diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 88e7174..dbf7060 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 1d2e20c..14710b0 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 59e7be0..928e27f 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 74a74ba..729ba2e 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 8fcf9a0..32e4817 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 494b1e6..e4347bf 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 2c3e075..69f61fb 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 09d8f0b..5a42ae1 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 21590fa..7c8864b 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 9bf8546..aa13d95 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 }