|
@@ -10,38 +10,38 @@ const listChats = async (req, res, next) => {
|
|
|
req.query
|
|
|
);
|
|
|
|
|
|
- const messages = await MessageModel.getLastMessagesList(userId);
|
|
|
+ // const messages = await MessageModel.getLastMessagesList(userId);
|
|
|
|
|
|
- const lastOnline = await chats.reduce(async (acc, { companionId }) => {
|
|
|
- const { _id, online, updatedAt } = await UserModel.findById(companionId);
|
|
|
- acc.push({ _id, online, updatedAt });
|
|
|
- return acc;
|
|
|
- }, []);
|
|
|
+ // const lastOnline = await chats.reduce(async (acc, { companionId }) => {
|
|
|
+ // const { _id, online, updatedAt } = await UserModel.findById(companionId);
|
|
|
+ // acc.push({ _id, online, updatedAt });
|
|
|
+ // return acc;
|
|
|
+ // }, []);
|
|
|
|
|
|
- const lastMessages = await messages.reduce(
|
|
|
- (acc, { message, companionId, createdAt, updatedAt }) => {
|
|
|
- const obj = {
|
|
|
- message,
|
|
|
- companionId,
|
|
|
- createdAt,
|
|
|
- updatedAt,
|
|
|
- };
|
|
|
- if (acc.length === 0) {
|
|
|
- acc.push(obj);
|
|
|
- return acc;
|
|
|
- }
|
|
|
- if (acc.some((el) => el.companionId === companionId)) {
|
|
|
- const i = acc.findIndex((el) => {
|
|
|
- return el.companionId === companionId;
|
|
|
- });
|
|
|
- acc[i] = obj;
|
|
|
- return acc;
|
|
|
- }
|
|
|
- acc.push(obj);
|
|
|
- return acc;
|
|
|
- },
|
|
|
- []
|
|
|
- );
|
|
|
+ // const lastMessages = await messages.reduce(
|
|
|
+ // (acc, { message, companionId, createdAt, updatedAt }) => {
|
|
|
+ // const obj = {
|
|
|
+ // message,
|
|
|
+ // companionId,
|
|
|
+ // createdAt,
|
|
|
+ // updatedAt,
|
|
|
+ // };
|
|
|
+ // if (acc.length === 0) {
|
|
|
+ // acc.push(obj);
|
|
|
+ // return acc;
|
|
|
+ // }
|
|
|
+ // if (acc.some((el) => el.companionId === companionId)) {
|
|
|
+ // const i = acc.findIndex((el) => {
|
|
|
+ // return el.companionId === companionId;
|
|
|
+ // });
|
|
|
+ // acc[i] = obj;
|
|
|
+ // return acc;
|
|
|
+ // }
|
|
|
+ // acc.push(obj);
|
|
|
+ // return acc;
|
|
|
+ // },
|
|
|
+ // []
|
|
|
+ // );
|
|
|
|
|
|
return res.json({
|
|
|
status: 'success',
|
|
@@ -51,8 +51,8 @@ const listChats = async (req, res, next) => {
|
|
|
limit,
|
|
|
page,
|
|
|
chats,
|
|
|
- lastMessages,
|
|
|
- lastOnline,
|
|
|
+ lastMessages: [{ message: 'fff' }, { message: 'ddd' }],
|
|
|
+ lastOnline: [new Date(), new Date()],
|
|
|
},
|
|
|
});
|
|
|
} catch (e) {
|
|
@@ -77,7 +77,7 @@ const startChat = async (req, res, next) => {
|
|
|
online: Online,
|
|
|
number: Number,
|
|
|
} = user;
|
|
|
- if (companion && (isUser || isCompanion)) {
|
|
|
+ if (companion && isUser && isCompanion) {
|
|
|
await ChatModel.update(isUser._id, {
|
|
|
avatarUrl,
|
|
|
color,
|
|
@@ -98,31 +98,88 @@ const startChat = async (req, res, next) => {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- if (companion && !isUser && !isCompanion) {
|
|
|
- const newChat = await ChatModel.add({
|
|
|
- name,
|
|
|
- lastName,
|
|
|
- avatarUrl,
|
|
|
- color,
|
|
|
- online,
|
|
|
- number,
|
|
|
- companionId: id,
|
|
|
- owner: userId,
|
|
|
- });
|
|
|
- await ChatModel.add({
|
|
|
- name: Name,
|
|
|
- lastName: LastName,
|
|
|
- avatarUrl: AvatarUrl,
|
|
|
- color: Color,
|
|
|
- online: Online,
|
|
|
- number: Number,
|
|
|
- companionId: userId,
|
|
|
- owner: id,
|
|
|
- });
|
|
|
+ if (companion) {
|
|
|
+ const newChat =
|
|
|
+ !isUser &&
|
|
|
+ (await ChatModel.add({
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ online,
|
|
|
+ number,
|
|
|
+ companionId: id,
|
|
|
+ owner: userId,
|
|
|
+ }));
|
|
|
+ !isCompanion &&
|
|
|
+ (await ChatModel.add({
|
|
|
+ name: Name,
|
|
|
+ lastName: LastName,
|
|
|
+ avatarUrl: AvatarUrl,
|
|
|
+ color: Color,
|
|
|
+ online: Online,
|
|
|
+ number: Number,
|
|
|
+ companionId: userId,
|
|
|
+ owner: id,
|
|
|
+ }));
|
|
|
return res.status(201).json({
|
|
|
status: 'success',
|
|
|
code: 201,
|
|
|
- data: newChat,
|
|
|
+ data: newChat ? newChat : isUser,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const removeChatForBoth = async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const companionId = req.params.id;
|
|
|
+ const userId = req.user.id;
|
|
|
+ const isUserChat = await ChatModel.getByField(companionId, userId);
|
|
|
+ const isCompanionChat = await ChatModel.getByField(userId, companionId);
|
|
|
+ if (isUserChat || isCompanionChat) {
|
|
|
+ isUserChat && (await MessageModel.removeAll(userId));
|
|
|
+ isCompanionChat && (await MessageModel.removeAll(companionId));
|
|
|
+ isUserChat && (await ChatModel.remove(isUserChat._id, userId));
|
|
|
+ isCompanionChat &&
|
|
|
+ (await ChatModel.remove(isCompanionChat._id, companionId));
|
|
|
+ return res.json({
|
|
|
+ status: 'success',
|
|
|
+ code: 200,
|
|
|
+ data: {},
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return res.status(404).json({
|
|
|
+ status: 'error',
|
|
|
+ code: 404,
|
|
|
+ data: 'Not Found',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const removeChatForMe = async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const companionId = req.params.id;
|
|
|
+ const userId = req.user.id;
|
|
|
+ const isUserChat = await ChatModel.getByField(companionId, userId);
|
|
|
+ if (isUserChat) {
|
|
|
+ await MessageModel.removeAll(userId);
|
|
|
+ await ChatModel.remove(isUserChat._id, userId);
|
|
|
+ return res.json({
|
|
|
+ status: 'success',
|
|
|
+ code: 200,
|
|
|
+ data: {},
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return res.status(404).json({
|
|
|
+ status: 'error',
|
|
|
+ code: 404,
|
|
|
+ data: 'Not Found',
|
|
|
});
|
|
|
}
|
|
|
} catch (e) {
|
|
@@ -267,6 +324,8 @@ const chatById = async (req, res, next) => {
|
|
|
module.exports = {
|
|
|
listChats,
|
|
|
startChat,
|
|
|
+ removeChatForBoth,
|
|
|
+ removeChatForMe,
|
|
|
muteChat,
|
|
|
sortChat,
|
|
|
seenChat,
|