Skip to content

Commit

Permalink
feat: ✨ lock and unlock projects according to their org status
Browse files Browse the repository at this point in the history
  • Loading branch information
clairenollet committed Jul 21, 2023
1 parent 131481a commit 3b9444a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
60 changes: 58 additions & 2 deletions apps/client/cypress/e2e/specs/admin/organizations.e2e.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getModel } from '../../support/func.js'
import { getModel, getProjectById } from '../../support/func.js'

describe('Administration organizations', () => {
const organizations = getModel('organization').map(({ label, name, active }) => ({
const organizations = getModel('organization').map(({ id, label, name, active }) => ({
id,
label,
name,
active,
Expand All @@ -16,6 +17,7 @@ describe('Administration organizations', () => {

beforeEach(() => {
cy.intercept('GET', 'api/v1/admin/organizations').as('getAllOrganizations')
cy.intercept('GET', 'api/v1/projects').as('getProjects')

cy.kcLogin('tcolin')
cy.visit('/admin/organizations')
Expand Down Expand Up @@ -102,6 +104,60 @@ describe('Administration organizations', () => {
.should('not.exist')
})

it('Should deactivate and activate an organization with impact on its projects', () => {
const projectFailed = getProjectById('83833faf-f654-40dd-bcd5-cf2e944fc702')
const projectSucceed = getProjectById('011e7860-04d7-461f-912d-334c622d38b3')
const organization = organizations.find(organization => organization.id === projectFailed.organizationId)

cy.getByDataTestid('tableAdministrationOrganizations').within(() => {
cy.getByDataTestid(`${organization.name}-active-cbx`)
.should('be.checked')
.uncheck()
.blur()
})
cy.getByDataTestid('snackbar').within(() => {
cy.get('p').should('contain', `Organisation ${organization.name} mise à jour`)
})

cy.visit('/projects')
.wait('@getProjects')
cy.getByDataTestid(`projectTile-${projectFailed.name}`)
.click()
cy.getByDataTestid(`${projectFailed.id}-locked-badge`)
.should('exist')

cy.visit('/projects')
.wait('@getProjects')
cy.getByDataTestid(`projectTile-${projectSucceed.name}`)
.click()
cy.getByDataTestid(`${projectSucceed.id}-locked-badge`)
.should('exist')

cy.visit('/admin/organizations')
.wait('@getAllOrganizations')
cy.getByDataTestid('tableAdministrationOrganizations').within(() => {
cy.getByDataTestid(`${organization.name}-active-cbx`)
.should('not.be.checked')
.check()
.blur()
})
cy.getByDataTestid('snackbar').within(() => {
cy.get('p').should('contain', `Organisation ${organization.name} mise à jour`)
})

cy.visit('/projects')
cy.getByDataTestid(`projectTile-${projectFailed.name}`)
.click()
cy.getByDataTestid(`${projectFailed.id}-locked-badge`)
.should('exist')

cy.visit('/projects')
cy.getByDataTestid(`projectTile-${projectSucceed.name}`)
.click()
cy.getByDataTestid(`${projectSucceed.id}-locked-badge`)
.should('not.exist')
})

it('Should synchronize organizations from plugins', () => {
cy.intercept('PUT', '/api/v1/admin/organizations/sync/organizations').as('syncOrganizations')

Expand Down
20 changes: 19 additions & 1 deletion apps/server/src/controllers/admin/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { sendOk, sendCreated, sendNotFound, sendBadRequest } from '../../utils/r
import { hooks } from '../../plugins/index.js'
import { HookPayload, PluginResult } from '@/plugins/hooks/hook.js'
import { objectValues } from '@/utils/type.js'
import { getProjectByOrganizationId, lockProject } from '@/queries/project-queries.js'
import { unlockProjectIfNotFailed } from '@/utils/controller.js'

// GET
export const getAllOrganizationsController = async (req, res) => {
Expand Down Expand Up @@ -70,13 +72,29 @@ export const updateOrganizationController = async (req, res) => {
const { active, label, source } = req.body

try {
let organization = await getOrganizationByName(name)

if (active !== undefined) {
await updateActiveOrganization({ name, active })
/** lock project if organization becomes inactive */
if (active === false) {
const projects = await getProjectByOrganizationId(organization.id)
for (const project of projects) {
await lockProject(project.id)
}
}
/** unlock project if organization becomes active and no resource is failed */
if (active === true) {
const projects = await getProjectByOrganizationId(organization.id)
for (const project of projects) {
await unlockProjectIfNotFailed(project.id)
}
}
}
if (label) {
await updateLabelOrganization({ name, label, source })
}
const organization = await getOrganizationByName(name)
organization = await getOrganizationByName(name)

addReqLogs({
req,
Expand Down
11 changes: 11 additions & 0 deletions apps/server/src/queries/project-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ export const getProjectByNames = async ({ name, organizationName }: { name: Proj
return res
}

export const getProjectByOrganizationId = async (organizationId: Organization['id']) => {
return prisma.project.findMany({
where: {
organizationId,
status: {
not: 'archived',
},
},
})
}

// CREATE
export const initializeProject = async ({ name, organizationId, description = '', ownerId }: { name: Project['name'], organizationId: Organization['id'], description: Project['description'], ownerId: User['id'] }) => {
return prisma.project.create({
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const checkAdminGroup = (req, res, done) => {
done()
}

export const unlockProjectIfNotFailed = async (projectId) => {
export const unlockProjectIfNotFailed = async (projectId: Project['id']) => {
const ressources = [
...(await getEnvironmentsByProjectId(projectId))?.map(environment => environment.status),
...(await getProjectRepositories(projectId))?.map(repository => repository.status),
Expand Down
7 changes: 7 additions & 0 deletions packages/test-utils/src/imports/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ export const data = {
},
],
role: [
{
userId: 'cb8e5b4b-7b7b-40f5-935f-594f48ae6566',
projectId: '83833faf-f654-40dd-bcd5-cf2e944fc702',
role: 'user',
createdAt: '2023-07-03T14:46:56.826Z',
updatedAt: '2023-07-03T14:46:56.826Z',
},
{
userId: 'cb8e5b4b-7b7b-40f5-935f-594f48ae6565',
projectId: '22e7044f-8414-435d-9c4a-2df42a65034b',
Expand Down

0 comments on commit 3b9444a

Please sign in to comment.