好友:初步申请到查看链
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, mongoose, Ref } from '@typegoose/typegoose';
|
||||
import Role from './Role';
|
||||
import FriendShip from './FriendShip';
|
||||
import Role, { RoleType } from './Role';
|
||||
import FriendShip, { FriendShipType } from './FriendShip';
|
||||
import { ROLE } from '../consts';
|
||||
|
||||
class Relation {
|
||||
@prop({ required: true, default: '' })
|
||||
@@ -26,9 +27,36 @@ export default class FriendRelation extends BaseModel {
|
||||
@prop({ required: true, default: [], type: Relation, _id: false })
|
||||
blacklist: Relation[]; // 黑名单列表
|
||||
|
||||
|
||||
// 创建
|
||||
public static async addFriend(roleId: string, friend: RoleType, friendShip: FriendShipType) {
|
||||
|
||||
const result: FriendRelationType = await FriendRelationModel.findOneAndUpdate(
|
||||
{ roleId }, {
|
||||
$setOnInsert: { blacklist: [] },
|
||||
$push: { friends: { roleId: friend.roleId, role: friend._id, friendShip: friendShip._id } }
|
||||
}, { upsert: true, new: true }).lean();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// 获取列表
|
||||
public static async findFriendByRole(roleId: string) {
|
||||
const result: FriendRelationType = await FriendRelationModel.findOne({ roleId })
|
||||
.populate('friends.role', ROLE.SHOW_SIMPLE, 'Role')
|
||||
.populate('friends.friendShip', null, 'FriendShip')
|
||||
.lean({ getters: true, virtuals: true });
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async deleteAccount(roleId: string) {
|
||||
let result = await FriendRelationModel.deleteMany({ roleId });
|
||||
await FriendRelationModel.updateMany({}, { $pull: { friends: { roleId }, blacklist: { roleId } }});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const FriendRelationModel = getModelForClass(FriendRelation);
|
||||
|
||||
export interface FriendRelationType extends Pick<DocumentType<FriendRelation>, keyof FriendRelation>{};
|
||||
export interface FriendRelationType extends Pick<DocumentType<FriendRelation>, keyof FriendRelation> { };
|
||||
Reference in New Issue
Block a user