Last active
August 2, 2022 17:55
-
-
Save keske/6f9b04dbb044c24925dd90076fd4ddb7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { modelOptions, prop, getModelForClass } from '@typegoose/typegoose' | |
import { Field, ObjectType } from 'type-graphql' | |
import { BaseModel, User } from '.' | |
import { DateScalar } from '../types/lib' | |
@ObjectType() | |
@modelOptions({ | |
schemaOptions: { | |
collection: 'friendships', | |
timestamps: false, // turn off timestamps because we're using `acceptedAt`, `declinedAt`, `requestedAt` | |
}, | |
}) | |
export class Friendship extends BaseModel() { | |
@Field(() => User) | |
@prop({ required: true, ref: User }) | |
addressedTo!: User // I'm not sure about the name of this field | |
@Field(() => User) | |
@prop({ required: true, ref: User }) | |
requestedBy!: User | |
// Statuses | |
@Field(() => DateScalar, { nullable: true }) | |
@prop() | |
acceptedAt?: Date | |
@Field(() => DateScalar, { nullable: true }) | |
@prop() | |
blockedAt?: Date | |
@Field(() => DateScalar, { nullable: true }) | |
@prop() | |
declinedAt?: Date | |
@Field(() => DateScalar) | |
@prop({ required: true }) | |
requestedAt!: Date | |
// Virtual properties | |
@Field(() => Boolean, { nullable: true }) | |
get pending(): boolean { | |
const { acceptedAt, blockedAt, declinedAt, requestedAt } = | |
((this as any)._doc as Friendship) || this | |
return ( | |
Boolean(requestedAt) || !Boolean(acceptedAt || blockedAt || declinedAt) | |
) | |
} | |
} | |
export const FriendshipModel = getModelForClass(Friendship) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment