2025-01-13 19:45:52 -06:00
|
|
|
import { ExpressionBuilder } from 'kysely';
|
|
|
|
|
import { DB } from 'src/db';
|
2024-03-20 16:02:51 -05:00
|
|
|
import { UserEntity } from 'src/entities/user.entity';
|
2023-01-27 20:50:07 +00:00
|
|
|
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
|
|
|
|
|
|
2024-04-19 06:47:29 -04:00
|
|
|
@Entity('sessions')
|
|
|
|
|
export class SessionEntity {
|
2023-01-27 20:50:07 +00:00
|
|
|
@PrimaryGeneratedColumn('uuid')
|
|
|
|
|
id!: string;
|
|
|
|
|
|
|
|
|
|
@Column({ select: false })
|
|
|
|
|
token!: string;
|
|
|
|
|
|
2023-04-25 22:19:23 -04:00
|
|
|
@Column()
|
|
|
|
|
userId!: string;
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
@ManyToOne(() => UserEntity, { onUpdate: 'CASCADE', onDelete: 'CASCADE' })
|
2023-01-27 20:50:07 +00:00
|
|
|
user!: UserEntity;
|
|
|
|
|
|
|
|
|
|
@CreateDateColumn({ type: 'timestamptz' })
|
2023-04-25 22:19:23 -04:00
|
|
|
createdAt!: Date;
|
2023-01-27 20:50:07 +00:00
|
|
|
|
|
|
|
|
@UpdateDateColumn({ type: 'timestamptz' })
|
2023-04-25 22:19:23 -04:00
|
|
|
updatedAt!: Date;
|
|
|
|
|
|
|
|
|
|
@Column({ default: '' })
|
|
|
|
|
deviceType!: string;
|
|
|
|
|
|
|
|
|
|
@Column({ default: '' })
|
|
|
|
|
deviceOS!: string;
|
2023-01-27 20:50:07 +00:00
|
|
|
}
|
2025-01-13 19:45:52 -06:00
|
|
|
|
|
|
|
|
const userColumns = [
|
|
|
|
|
'id',
|
|
|
|
|
'email',
|
|
|
|
|
'createdAt',
|
|
|
|
|
'profileImagePath',
|
|
|
|
|
'isAdmin',
|
|
|
|
|
'shouldChangePassword',
|
|
|
|
|
'deletedAt',
|
|
|
|
|
'oauthId',
|
|
|
|
|
'updatedAt',
|
|
|
|
|
'storageLabel',
|
|
|
|
|
'name',
|
|
|
|
|
'quotaSizeInBytes',
|
|
|
|
|
'quotaUsageInBytes',
|
|
|
|
|
'status',
|
|
|
|
|
'profileChangedAt',
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
export const withUser = (eb: ExpressionBuilder<DB, 'sessions'>) => {
|
|
|
|
|
return eb
|
|
|
|
|
.selectFrom('users')
|
|
|
|
|
.select(userColumns)
|
|
|
|
|
.select((eb) =>
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom('user_metadata')
|
|
|
|
|
.whereRef('users.id', '=', 'user_metadata.userId')
|
|
|
|
|
.select((eb) => eb.fn('array_agg', [eb.table('user_metadata')]).as('metadata'))
|
|
|
|
|
.as('metadata'),
|
|
|
|
|
)
|
|
|
|
|
.whereRef('users.id', '=', 'sessions.userId')
|
|
|
|
|
.where('users.deletedAt', 'is', null)
|
|
|
|
|
.as('user');
|
|
|
|
|
};
|