mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 10:27:26 +00:00
Merge pull request #2544 from Infisical/daniel/project-env-position-fixes
fix: project environment positions
This commit is contained in:
@@ -123,7 +123,7 @@ describe("Project Environment Router", async () => {
|
|||||||
id: deletedProjectEnvironment.id,
|
id: deletedProjectEnvironment.id,
|
||||||
name: mockProjectEnv.name,
|
name: mockProjectEnv.name,
|
||||||
slug: mockProjectEnv.slug,
|
slug: mockProjectEnv.slug,
|
||||||
position: 4,
|
position: 5,
|
||||||
createdAt: expect.any(String),
|
createdAt: expect.any(String),
|
||||||
updatedAt: expect.any(String)
|
updatedAt: expect.any(String)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -533,7 +533,8 @@ export const ENVIRONMENTS = {
|
|||||||
CREATE: {
|
CREATE: {
|
||||||
workspaceId: "The ID of the project to create the environment in.",
|
workspaceId: "The ID of the project to create the environment in.",
|
||||||
name: "The name of the environment to create.",
|
name: "The name of the environment to create.",
|
||||||
slug: "The slug of the environment to create."
|
slug: "The slug of the environment to create.",
|
||||||
|
position: "The position of the environment. The lowest number will be displayed as the first environment."
|
||||||
},
|
},
|
||||||
UPDATE: {
|
UPDATE: {
|
||||||
workspaceId: "The ID of the project to update the environment in.",
|
workspaceId: "The ID of the project to update the environment in.",
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ export const registerProjectEnvRouter = async (server: FastifyZodProvider) => {
|
|||||||
}),
|
}),
|
||||||
body: z.object({
|
body: z.object({
|
||||||
name: z.string().trim().describe(ENVIRONMENTS.CREATE.name),
|
name: z.string().trim().describe(ENVIRONMENTS.CREATE.name),
|
||||||
|
position: z.number().min(1).optional().describe(ENVIRONMENTS.CREATE.position),
|
||||||
slug: z
|
slug: z
|
||||||
.string()
|
.string()
|
||||||
.trim()
|
.trim()
|
||||||
|
|||||||
@@ -65,10 +65,16 @@ export const projectEnvDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const shiftPositions = async (projectId: string, pos: number, tx?: Knex) => {
|
||||||
|
// Shift all positions >= the new position up by 1
|
||||||
|
await (tx || db)(TableName.Environment).where({ projectId }).where("position", ">=", pos).increment("position", 1);
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...projectEnvOrm,
|
...projectEnvOrm,
|
||||||
findBySlugs,
|
findBySlugs,
|
||||||
findLastEnvPosition,
|
findLastEnvPosition,
|
||||||
updateAllPosition
|
updateAllPosition,
|
||||||
|
shiftPositions
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export const projectEnvServiceFactory = ({
|
|||||||
actor,
|
actor,
|
||||||
actorOrgId,
|
actorOrgId,
|
||||||
actorAuthMethod,
|
actorAuthMethod,
|
||||||
|
position,
|
||||||
name,
|
name,
|
||||||
slug
|
slug
|
||||||
}: TCreateEnvDTO) => {
|
}: TCreateEnvDTO) => {
|
||||||
@@ -83,9 +84,25 @@ export const projectEnvServiceFactory = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const env = await projectEnvDAL.transaction(async (tx) => {
|
const env = await projectEnvDAL.transaction(async (tx) => {
|
||||||
|
if (position !== undefined) {
|
||||||
|
// Check if there's an environment at the specified position
|
||||||
|
const existingEnvWithPosition = await projectEnvDAL.findOne({ projectId, position }, tx);
|
||||||
|
|
||||||
|
// If there is, then shift positions
|
||||||
|
if (existingEnvWithPosition) {
|
||||||
|
await projectEnvDAL.shiftPositions(projectId, position, tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
const doc = await projectEnvDAL.create({ slug, name, projectId, position }, tx);
|
||||||
|
await folderDAL.create({ name: "root", parentId: null, envId: doc.id, version: 1 }, tx);
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
// If no position is specified, add to the end
|
||||||
const lastPos = await projectEnvDAL.findLastEnvPosition(projectId, tx);
|
const lastPos = await projectEnvDAL.findLastEnvPosition(projectId, tx);
|
||||||
const doc = await projectEnvDAL.create({ slug, name, projectId, position: lastPos + 1 }, tx);
|
const doc = await projectEnvDAL.create({ slug, name, projectId, position: lastPos + 1 }, tx);
|
||||||
await folderDAL.create({ name: "root", parentId: null, envId: doc.id, version: 1 }, tx);
|
await folderDAL.create({ name: "root", parentId: null, envId: doc.id, version: 1 }, tx);
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -150,7 +167,11 @@ export const projectEnvServiceFactory = ({
|
|||||||
|
|
||||||
const env = await projectEnvDAL.transaction(async (tx) => {
|
const env = await projectEnvDAL.transaction(async (tx) => {
|
||||||
if (position) {
|
if (position) {
|
||||||
await projectEnvDAL.updateAllPosition(projectId, oldEnv.position, position, tx);
|
const existingEnvWithPosition = await projectEnvDAL.findOne({ projectId, position }, tx);
|
||||||
|
|
||||||
|
if (existingEnvWithPosition && existingEnvWithPosition.id !== oldEnv.id) {
|
||||||
|
await projectEnvDAL.updateAllPosition(projectId, oldEnv.position, position, tx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return projectEnvDAL.updateById(oldEnv.id, { name, slug, position }, tx);
|
return projectEnvDAL.updateById(oldEnv.id, { name, slug, position }, tx);
|
||||||
});
|
});
|
||||||
@@ -199,7 +220,6 @@ export const projectEnvServiceFactory = ({
|
|||||||
name: "DeleteEnvironment"
|
name: "DeleteEnvironment"
|
||||||
});
|
});
|
||||||
|
|
||||||
await projectEnvDAL.updateAllPosition(projectId, doc.position, -1, tx);
|
|
||||||
return doc;
|
return doc;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { TProjectPermission } from "@app/lib/types";
|
|||||||
export type TCreateEnvDTO = {
|
export type TCreateEnvDTO = {
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
|
position?: number;
|
||||||
} & TProjectPermission;
|
} & TProjectPermission;
|
||||||
|
|
||||||
export type TUpdateEnvDTO = {
|
export type TUpdateEnvDTO = {
|
||||||
|
|||||||
Reference in New Issue
Block a user