Skip to content

The open alternative to Salesforce, designed for AI.

Add migration to restrict users without workspaces (#5369)

martmull8576127b47ab
diff --git a/packages/twenty-server/src/database/typeorm/core/migrations/1715593226719-updateInconsistentUserConstraint.ts b/packages/twenty-server/src/database/typeorm/core/migrations/1715593226719-updateInconsistentUserConstraint.ts
new file mode 100644
index 00000000000..ace85a0c542
--- /dev/null
+++ b/packages/twenty-server/src/database/typeorm/core/migrations/1715593226719-updateInconsistentUserConstraint.ts
@@ -0,0 +1,43 @@
+import { MigrationInterface, QueryRunner } from 'typeorm';
+
+export class UpdateInconsistentUserConstraint1715593226719
+ implements MigrationInterface
+{
+ name = 'UpdateInconsistentUserConstraint1715593226719';
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ `ALTER TABLE "core"."user" DROP CONSTRAINT "FK_2ec910029395fa7655621c88908"`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."billingSubscription" ALTER COLUMN "status" TYPE text`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."billingSubscription" ALTER COLUMN "interval" TYPE text`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."workspace" ALTER COLUMN "subscriptionStatus" TYPE text`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."user" ADD CONSTRAINT "FK_2ec910029395fa7655621c88908" FOREIGN KEY ("defaultWorkspaceId") REFERENCES "core"."workspace"("id") ON DELETE RESTRICT ON UPDATE NO ACTION`,
+ );
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ `ALTER TABLE "core"."user" DROP CONSTRAINT "FK_2ec910029395fa7655621c88908"`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."workspace" ALTER COLUMN "subscriptionStatus" TYPE character varying`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."billingSubscription" ALTER COLUMN "interval" TYPE character varying`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."billingSubscription" ALTER COLUMN "status" TYPE character varying`,
+ );
+ await queryRunner.query(
+ `ALTER TABLE "core"."user" ADD CONSTRAINT "FK_2ec910029395fa7655621c88908" FOREIGN KEY ("defaultWorkspaceId") REFERENCES "core"."workspace"("id") ON DELETE SET NULL ON UPDATE NO ACTION`,
+ );
+ }
+}
diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts
index 1e5d639963a..3c405a62b92 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/services/token.service.spec.ts
@@ -160,6 +160,9 @@ describe('TokenService', () => {
};
const mockUser = { id: '1', email: '[email protected]' };
+ const mockedNewDate = new Date();
+
+ jest.spyOn(global, 'Date').mockImplementation(() => mockedNewDate);
jest
.spyOn(appTokenRepository, 'findOne')
.mockResolvedValue(mockToken as AppToken);
@@ -173,7 +176,7 @@ describe('TokenService', () => {
where: {
value: hashedToken,
type: AppTokenType.PasswordResetToken,
- expiresAt: MoreThan(new Date()),
+ expiresAt: MoreThan(mockedNewDate),
revokedAt: IsNull(),
},
});
diff --git a/packages/twenty-server/src/engine/core-modules/user/user.entity.ts b/packages/twenty-server/src/engine/core-modules/user/user.entity.ts
index 993d6ed1bc5..a4e66559434 100644
--- a/packages/twenty-server/src/engine/core-modules/user/user.entity.ts
+++ b/packages/twenty-server/src/engine/core-modules/user/user.entity.ts
@@ -71,7 +71,7 @@ export class User {
@Field(() => Workspace, { nullable: false })
@ManyToOne(() => Workspace, (workspace) => workspace.users, {
- onDelete: 'SET NULL',
+ onDelete: 'RESTRICT',
})
defaultWorkspace: Relation<Workspace>;