Files
outline/server/models/View.ts
Apoorv Mishra 7e61a519f1 Type server models (#6326)
* fix: type server models

* fix: make ParanoidModel generic

* fix: ApiKey

* fix: Attachment

* fix: AuthenticationProvider

* fix: Backlink

* fix: Collection

* fix: Comment

* fix: Document

* fix: FileOperation

* fix: Group

* fix: GroupPermission

* fix: GroupUser

* fix: Integration

* fix: IntegrationAuthentication

* fix: Notification

* fix: Pin

* fix: Revision

* fix: SearchQuery

* fix: Share

* fix: Star

* fix: Subscription

* fix: TypeError

* fix: Imports

* fix: Team

* fix: TeamDomain

* fix: User

* fix: UserAuthentication

* fix: UserPermission

* fix: View

* fix: WebhookDelivery

* fix: WebhookSubscription

* Remove type duplication

---------

Co-authored-by: Tom Moor <tom.moor@gmail.com>
2024-01-12 22:33:05 +05:30

122 lines
2.2 KiB
TypeScript

import {
FindOrCreateOptions,
InferAttributes,
InferCreationAttributes,
Op,
} from "sequelize";
import {
BelongsTo,
Column,
Default,
ForeignKey,
Table,
DataType,
Scopes,
} from "sequelize-typescript";
import Document from "./Document";
import User from "./User";
import IdModel from "./base/IdModel";
import Fix from "./decorators/Fix";
@Scopes(() => ({
withUser: () => ({
include: [
{
model: User,
required: true,
as: "user",
},
],
}),
}))
@Table({ tableName: "views", modelName: "view" })
@Fix
class View extends IdModel<
InferAttributes<View>,
Partial<InferCreationAttributes<View>>
> {
@Column
lastEditingAt: Date | null;
@Default(1)
@Column(DataType.INTEGER)
count: number;
// associations
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
userId: string;
@BelongsTo(() => Document, "documentId")
document: Document;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
static async incrementOrCreate(
where: {
userId: string;
documentId: string;
},
options?: FindOrCreateOptions<InferAttributes<View>>
) {
const [model, created] = await this.findOrCreate({
...options,
where,
});
if (!created) {
model.count += 1;
await model.save(options);
}
return model;
}
static async findByDocument(
documentId: string,
{ includeSuspended }: { includeSuspended?: boolean }
) {
return this.findAll({
where: {
documentId,
},
order: [["updatedAt", "DESC"]],
include: [
{
model: User,
required: true,
...(includeSuspended
? {}
: { where: { suspendedAt: { [Op.is]: null } } }),
},
],
});
}
static async touch(documentId: string, userId: string, isEditing: boolean) {
const values: Partial<View> = {
updatedAt: new Date(),
};
if (isEditing) {
values.lastEditingAt = new Date();
}
await this.update(values, {
where: {
userId,
documentId,
},
returning: false,
});
}
}
export default View;