Skip to content

Commit 85ec301

Browse files
Assign color for each project
1 parent 0c299e4 commit 85ec301

5 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { PROJECT_COLORS } from '@openops/shared';
2+
import { MigrationInterface, QueryRunner } from 'typeorm';
3+
4+
export class AddColorToProject1765287443041 implements MigrationInterface {
5+
name = 'AddColorToProject1765287443041';
6+
7+
public async up(queryRunner: QueryRunner): Promise<void> {
8+
await queryRunner.query(`
9+
ALTER TABLE "project"
10+
ADD COLUMN IF NOT EXISTS "color" character varying;
11+
`);
12+
13+
const projects = await queryRunner.query(`
14+
SELECT "id" FROM "project" WHERE "color" IS NULL ORDER BY "created" ASC;
15+
`);
16+
17+
for (let i = 0; i < projects.length; i++) {
18+
const color = PROJECT_COLORS[i % PROJECT_COLORS.length];
19+
await queryRunner.query(
20+
`UPDATE "project" SET "color" = $1 WHERE "id" = $2;`,
21+
[color, projects[i].id],
22+
);
23+
}
24+
25+
await queryRunner.query(`
26+
ALTER TABLE "project"
27+
ALTER COLUMN "color" SET NOT NULL;
28+
`);
29+
}
30+
31+
public async down(_: QueryRunner): Promise<void> {
32+
throw new Error('Rollback not implemented');
33+
}
34+
}

packages/server/api/src/app/database/postgres-connection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { AddTestRunActionLimitsToFlowVersion1760429290001 } from './migrations/1
3939
import { MoveTablesWorkspaceIdFromOrganizationToProject1760500000000 } from './migrations/1760500000000-MoveTablesWorkspaceIdFromOrganizationToProject';
4040
import { AddTablesDatabaseTokenToProject1763394159990 } from './migrations/1763394159990-AddTablesTokenToProject';
4141
import { MigrateTablesUserPassword1763755045436 } from './migrations/1763755045436-MigrateTablesUserPassword';
42+
import { AddColorToProject1765287443041 } from './migrations/1765287443041-AddColorToProject';
4243

4344
const getSslConfig = (): boolean | TlsOptions => {
4445
const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL);
@@ -86,6 +87,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
8687
MoveTablesWorkspaceIdFromOrganizationToProject1760500000000,
8788
AddTablesDatabaseTokenToProject1763394159990,
8889
MigrateTablesUserPassword1763755045436,
90+
AddColorToProject1765287443041,
8991
];
9092
};
9193

packages/server/api/src/app/project/project-entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export const ProjectEntity = new EntitySchema<ProjectSchema>({
5151
type: JSONB_COLUMN_TYPE,
5252
nullable: false,
5353
},
54+
color: {
55+
type: String,
56+
nullable: false,
57+
},
5458
},
5559
indices: [
5660
{

packages/server/api/src/app/project/project-service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
openOpsId,
88
OpenOpsId,
99
Project,
10+
PROJECT_COLORS,
11+
ProjectColor,
1012
ProjectId,
1113
spreadIfDefined,
1214
User,
@@ -19,6 +21,12 @@ import { projectHooks } from './project-hooks';
1921

2022
export const projectRepo = repoFactory(ProjectEntity);
2123

24+
async function getNextProjectColor(): Promise<ProjectColor> {
25+
const projectCount = await projectRepo().count();
26+
const colorIndex = projectCount % PROJECT_COLORS.length;
27+
return PROJECT_COLORS[colorIndex];
28+
}
29+
2230
export const projectService = {
2331
async create(params: CreateParams): Promise<Project> {
2432
const encryptTablesToken = encryptUtils.encryptString(
@@ -28,6 +36,7 @@ export const projectService = {
2836
id: openOpsId(),
2937
...params,
3038
tablesDatabaseToken: encryptTablesToken,
39+
color: await getNextProjectColor(),
3140
};
3241

3342
const savedProject = await projectRepo().save(newProject);

packages/shared/src/lib/project/project.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import { BaseModelSchema, Nullable } from '../common/base-model';
33
import { OpenOpsId } from '../common/id-generator';
44
import { ProjectMemberRole } from './project-member';
55

6+
export const PROJECT_COLORS = [
7+
'purple',
8+
'blue',
9+
'green',
10+
'yellow',
11+
'pink',
12+
] as const;
13+
14+
export type ProjectColor = (typeof PROJECT_COLORS)[number];
15+
16+
export const ProjectColorSchema = Type.Union(
17+
PROJECT_COLORS.map((color) => Type.Literal(color)),
18+
);
19+
620
export const ListProjectRequestForUserQueryParams = Type.Object({
721
cursor: Type.Optional(Type.String()),
822
limit: Type.Optional(Type.Number()),
@@ -38,6 +52,7 @@ export const Project = Type.Object({
3852
iv: Type.String(),
3953
data: Type.String(),
4054
}),
55+
color: ProjectColorSchema,
4156
});
4257

4358
export type Project = Static<typeof Project>;

0 commit comments

Comments
 (0)