forked from cerc-io/snowballtools-base
Add GQL mutation to update deployment to production (#39)
* Add mutation to update deployment to production * Implement gql client mutation and frontend to update deployment to production * Add toast message when deployment is changed to production * Throw error from init db script if db aleardy exists --------- Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
committed by
Ashwin Phatak
co-authored by
neeraj
parent
44310d4eb8
commit
2fb048e8ab
@@ -1,4 +1,4 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { DataSource, DeepPartial } from 'typeorm';
|
||||
import path from 'path';
|
||||
import debug from 'debug';
|
||||
import assert from 'assert';
|
||||
@@ -131,8 +131,7 @@ export class Database {
|
||||
return environmentVariables;
|
||||
}
|
||||
|
||||
async removeProjectMemberByMemberId (memberId: string): Promise<boolean> {
|
||||
// TODO: Check if user is authorized to delete members
|
||||
async removeProjectMemberById (memberId: string): Promise<boolean> {
|
||||
const projectMemberRepository = this.dataSource.getRepository(ProjectMember);
|
||||
|
||||
const deleted = await projectMemberRepository.delete(memberId);
|
||||
@@ -206,4 +205,15 @@ export class Database {
|
||||
|
||||
return projects;
|
||||
}
|
||||
|
||||
async updateDeploymentById (deploymentId: string, updates: DeepPartial<Deployment>): Promise<boolean> {
|
||||
const deploymentRepository = this.dataSource.getRepository(Deployment);
|
||||
const updatedDeployment = await deploymentRepository.update({ id: Number(deploymentId) }, updates);
|
||||
|
||||
if (updatedDeployment.affected) {
|
||||
return updatedDeployment.affected > 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
import { Project } from './Project';
|
||||
import { Domain } from './Domain';
|
||||
|
||||
enum Environment {
|
||||
export enum Environment {
|
||||
Production = 'Production',
|
||||
Preview = 'Preview',
|
||||
Development = 'Development',
|
||||
|
||||
@@ -3,6 +3,7 @@ import assert from 'assert';
|
||||
|
||||
import { Database } from './database';
|
||||
import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType, isUserOwner } from './utils';
|
||||
import { Environment } from './entity/Deployment';
|
||||
|
||||
const log = debug('snowball:database');
|
||||
|
||||
@@ -102,7 +103,7 @@ export const createResolvers = async (db: Database): Promise<any> => {
|
||||
assert(memberProject);
|
||||
|
||||
if (isUserOwner(String(context.userId), String(memberProject.owner.id))) {
|
||||
return db.removeProjectMemberByMemberId(memberId);
|
||||
return db.removeProjectMemberById(memberId);
|
||||
} else {
|
||||
throw new Error('Invalid operation: not authorized');
|
||||
}
|
||||
@@ -119,6 +120,17 @@ export const createResolvers = async (db: Database): Promise<any> => {
|
||||
log(err);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
updateDeploymentToProd: async (_: any, { deploymentId }: {deploymentId: string }) => {
|
||||
try {
|
||||
return db.updateDeploymentById(deploymentId, {
|
||||
environment: Environment.Production
|
||||
});
|
||||
} catch (err) {
|
||||
log(err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -124,6 +124,7 @@ type Query {
|
||||
type Mutation {
|
||||
removeMember(memberId: String!): Boolean!
|
||||
addEnvironmentVariables(projectId: String!, environmentVariables: [AddEnvironmentVariableInput!]): Boolean!
|
||||
updateDeploymentToProd(deploymentId: String!): Boolean!
|
||||
}
|
||||
|
||||
input AddEnvironmentVariableInput {
|
||||
|
||||
@@ -117,6 +117,8 @@ const main = async () => {
|
||||
await dataSource.initialize();
|
||||
|
||||
await generateTestData(dataSource);
|
||||
} else {
|
||||
throw new Error('Database already exists');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user