PeerTube/server/core/models/user/user-import.ts

89 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-02-22 03:12:04 -06:00
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
2024-02-12 03:47:52 -06:00
import { MUserImport } from '@server/types/models/index.js'
2024-02-22 03:12:04 -06:00
import { SequelizeModel } from '../shared/index.js'
2024-02-12 03:47:52 -06:00
import { UserModel } from './user.js'
import type { UserImportResultSummary, UserImportStateType } from '@peertube/peertube-models'
import { getSort } from '../shared/sort.js'
import { USER_IMPORT_STATES } from '@server/initializers/constants.js'
@Table({
tableName: 'userImport',
indexes: [
{
fields: [ 'userId' ]
},
{
fields: [ 'filename' ],
unique: true
}
]
})
2024-02-22 03:12:04 -06:00
export class UserImportModel extends SequelizeModel<UserImportModel> {
2024-02-12 03:47:52 -06:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column
filename: string
@AllowNull(false)
@Column
state: UserImportStateType
@AllowNull(true)
@Column(DataType.TEXT)
error: string
@AllowNull(true)
@Column(DataType.JSONB)
resultSummary: UserImportResultSummary
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
User: Awaited<UserModel>
static load (id: number | string) {
return UserImportModel.findByPk<MUserImport>(id)
}
static loadLatestByUserId (userId: number) {
return UserImportModel.findOne<MUserImport>({
where: {
userId
},
order: getSort('-createdAt')
})
}
// ---------------------------------------------------------------------------
generateAndSetFilename () {
if (!this.userId) throw new Error('Cannot generate filename without userId')
if (!this.createdAt) throw new Error('Cannot generate filename without createdAt')
this.filename = `user-import-${this.userId}-${this.createdAt.toISOString()}.zip`
}
toFormattedJSON () {
return {
id: this.id,
state: {
id: this.state,
label: USER_IMPORT_STATES[this.state]
},
createdAt: this.createdAt.toISOString()
}
}
}