sharkey/src/models/user-list.ts

42 lines
934 B
TypeScript
Raw Normal View History

2018-04-24 10:08:15 +09:00
import * as mongo from 'mongodb';
2018-06-18 09:54:53 +09:00
const deepcopy = require('deepcopy');
2018-04-24 10:08:15 +09:00
import db from '../db/mongodb';
2018-10-16 11:38:09 +09:00
import isObjectId from '../misc/is-objectid';
2018-04-24 10:08:15 +09:00
const UserList = db.get<IUserList>('userList');
export default UserList;
export interface IUserList {
_id: mongo.ObjectID;
createdAt: Date;
title: string;
userId: mongo.ObjectID;
userIds: mongo.ObjectID[];
}
2018-04-24 18:13:06 +09:00
export const pack = (
userList: string | mongo.ObjectID | IUserList
) => new Promise<any>(async (resolve, reject) => {
let _userList: any;
2018-10-16 11:38:09 +09:00
if (isObjectId(userList)) {
2018-04-24 18:13:06 +09:00
_userList = await UserList.findOne({
_id: userList
});
} else if (typeof userList === 'string') {
_userList = await UserList.findOne({
_id: new mongo.ObjectID(userList)
});
} else {
_userList = deepcopy(userList);
}
if (!_userList) throw `invalid userList arg ${userList}`;
// Rename _id to id
_userList.id = _userList._id;
delete _userList._id;
resolve(_userList);
});