-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into user-wallet-integrati…
…on-tests
- Loading branch information
Showing
53 changed files
with
2,118 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class UserOrganization1738679234042 implements MigrationInterface { | ||
name = 'UserOrganization1738679234042'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "user_organizations" ("id" SERIAL NOT NULL, "name" character varying(255), "role" integer NOT NULL, "status" integer NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "userId" integer NOT NULL, "organizationId" integer NOT NULL, CONSTRAINT "UQ_user_organizations" UNIQUE ("userId", "organizationId"), CONSTRAINT "PK_UO_id" PRIMARY KEY ("id"))`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "idx_UO_role_status" ON "user_organizations" ("role", "status")`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "idx_UO_name" ON "user_organizations" ("name")`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" ADD CONSTRAINT "FK_UO_user_id" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" ADD CONSTRAINT "FK_UO_organization_id" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
await queryRunner.query(` | ||
CREATE TRIGGER update_updated_at | ||
BEFORE UPDATE | ||
ON | ||
user_organizations | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE update_updated_at(); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP TRIGGER IF EXISTS update_updated_at ON user_organizations;`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" DROP CONSTRAINT "FK_UO_organization_id"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" DROP CONSTRAINT "FK_UO_user_id"`, | ||
); | ||
await queryRunner.query(`DROP INDEX "public"."idx_UO_name"`); | ||
await queryRunner.query(`DROP INDEX "public"."idx_UO_role_status"`); | ||
await queryRunner.query(`DROP TABLE "user_organizations"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod'; | ||
|
||
export type Row = z.infer<typeof RowSchema>; | ||
|
||
/** | ||
* Note: this is a base schema for all entities that are meant to be persisted to the database. | ||
* The 'id' field is a primary key, and the 'created_at' and 'updated_at' fields are timestamps. | ||
* These fields shouldn't be modified by the application, and should be managed by the database. | ||
*/ | ||
export const RowSchema = z.object({ | ||
id: z.number().int(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/datasources/users/entities/user-organizations.entity.db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Organization } from '@/datasources/organizations/entities/organizations.entity.db'; | ||
import { User } from '@/datasources/users/entities/users.entity.db'; | ||
import { databaseEnumTransformer } from '@/domain/common/utils/enum'; | ||
import { | ||
UserOrganizationRole, | ||
UserOrganizationStatus, | ||
} from '@/domain/users/entities/user-organization.entity'; | ||
import { | ||
Column, | ||
Entity, | ||
Index, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
} from 'typeorm'; | ||
|
||
@Entity('user_organizations') | ||
@Unique('UQ_user_organizations', ['user', 'organization']) | ||
@Index('idx_UO_name', ['name']) | ||
@Index('idx_UO_role_status', ['role', 'status']) | ||
export class UserOrganization { | ||
@PrimaryGeneratedColumn({ | ||
primaryKeyConstraintName: 'PK_UO_id', | ||
}) | ||
id!: number; | ||
|
||
@ManyToOne(() => User, (user: User) => user.id, { | ||
cascade: true, | ||
nullable: false, | ||
}) | ||
@JoinColumn({ | ||
foreignKeyConstraintName: 'FK_UO_user_id', | ||
}) | ||
user!: User; | ||
|
||
@ManyToOne( | ||
() => Organization, | ||
(organization: Organization) => organization.id, | ||
{ | ||
cascade: true, | ||
nullable: false, | ||
}, | ||
) | ||
@JoinColumn({ | ||
foreignKeyConstraintName: 'FK_UO_organization_id', | ||
}) | ||
organization!: Organization; | ||
|
||
@Column({ type: 'varchar', length: 255, nullable: true }) | ||
name!: string | null; | ||
|
||
// Postgres enums are string therefore we use integer | ||
@Column({ | ||
type: 'integer', | ||
transformer: databaseEnumTransformer(UserOrganizationRole), | ||
}) | ||
role!: keyof typeof UserOrganizationRole; | ||
|
||
// Postgres enums are string therefore we use integer | ||
@Column({ | ||
type: 'integer', | ||
transformer: databaseEnumTransformer(UserOrganizationStatus), | ||
}) | ||
status!: keyof typeof UserOrganizationStatus; | ||
|
||
@Column({ | ||
name: 'created_at', | ||
type: 'timestamp with time zone', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
update: false, | ||
}) | ||
createdAt!: Date; | ||
|
||
@Column({ | ||
name: 'updated_at', | ||
type: 'timestamp with time zone', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
update: false, | ||
}) | ||
updatedAt!: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ValueTransformer } from 'typeorm'; | ||
|
||
export function getEnumKey< | ||
T extends { [key: string]: number | string; [key: number]: string }, | ||
>(enumObj: T, value: number): keyof T { | ||
const key = enumObj[value]; | ||
if (typeof key !== 'string') { | ||
throw new Error(`Invalid enum value: ${value}`); | ||
} | ||
return key; | ||
} | ||
|
||
export const databaseEnumTransformer = < | ||
T extends { [key: string]: number | string; [key: number]: string }, | ||
>( | ||
enumObj: T, | ||
): ValueTransformer => { | ||
return { | ||
to: (value: keyof typeof enumObj) => enumObj[value], | ||
from: (value: number): keyof T => getEnumKey(enumObj, value), | ||
}; | ||
}; |
Oops, something went wrong.