add logic and more test to saveMessage
This commit is contained in:
parent
68e1dfe7d8
commit
aced3b8914
@ -22,13 +22,6 @@ function createAddress(elt) {
|
|||||||
return `${elt.mailbox}@${elt.host}`;
|
return `${elt.mailbox}@${elt.host}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initiateRoom(envelope, ownerId, messageId, isSeen) {
|
|
||||||
await createRoom(envelope.subject, ownerId, messageId).then(async (roomId) => {
|
|
||||||
// todo register members
|
|
||||||
await registerMessageInRoom(messageId, roomId, isSeen, envelope.date);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const roomType = {
|
const roomType = {
|
||||||
ROOM: 0,
|
ROOM: 0,
|
||||||
CHANNEL: 1,
|
CHANNEL: 1,
|
||||||
@ -104,7 +97,6 @@ class registerMessageInApp {
|
|||||||
await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date);
|
await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date);
|
||||||
await this.registerMembers(roomId);
|
await this.registerMembers(roomId);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createOrRegisterOnMembers(roomId) {
|
async createOrRegisterOnMembers(roomId) {
|
||||||
@ -134,10 +126,10 @@ class registerMessageInApp {
|
|||||||
} else {
|
} else {
|
||||||
// it is not a reply and not a dm
|
// it is not a reply and not a dm
|
||||||
// so it is a channel, which can be possibly a group
|
// so it is a channel, which can be possibly a group
|
||||||
initiateRoom(this.envelope, this.ownerId, this.messageId, this.isSeen);
|
this.initiateRoom(this.ownerId, roomType.ROOM);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await this.createOrRegisterOnExistence(this.ownerId, roomType.CHANNEL);
|
await this.createOrRegisterOnExistence(this.ownerId, roomType.ROOM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,34 +5,83 @@ const { registerMessageInApp, roomType } = require("../../mails/saveMessage");
|
|||||||
jest.mock("../../db/mail");
|
jest.mock("../../db/mail");
|
||||||
|
|
||||||
// todo mock db
|
// todo mock db
|
||||||
|
// new message from us
|
||||||
|
// to multiple people -> room
|
||||||
|
// if response has same member => group
|
||||||
|
// if response is dm => channel
|
||||||
|
// to one person => dm
|
||||||
|
|
||||||
|
// new message from other
|
||||||
|
// to only me -> room
|
||||||
|
// if no reply to multiple message => channel
|
||||||
|
// else => dm
|
||||||
|
// to multiple people -> room
|
||||||
|
// // make it better
|
||||||
|
// if multiple members reply -> group
|
||||||
|
// if only me reply -> channel
|
||||||
|
|
||||||
describe("saveMessage", () => {
|
describe("saveMessage", () => {
|
||||||
describe("is not a reply", () => {
|
describe("new first message", () => {
|
||||||
it("DM from user should be assigned to other user", async () => {
|
|
||||||
const users = generateUsers(2);
|
|
||||||
const attrs = generateAttrs({ from: [users[0].user], to: [users[1].user] });
|
|
||||||
|
|
||||||
getUserIdOfMailbox.mockReturnValue([{ user_id: users[0].id }]);
|
const users = generateUsers(5);
|
||||||
getAddresseId.mockImplementation((email) => {
|
const ownUser = users[0];
|
||||||
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
|
const messageId = 1;
|
||||||
return match.id;
|
const boxId = 1;
|
||||||
|
getUserIdOfMailbox.mockReturnValue([{ user_id: ownUser.id }]);
|
||||||
|
getAddresseId.mockImplementation((email) => {
|
||||||
|
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
|
||||||
|
return match.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("from us", () => {
|
||||||
|
it("new first message from us to one recipient should create a DM", async () => {
|
||||||
|
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] });
|
||||||
|
|
||||||
|
const register = new registerMessageInApp(messageId, attrs, boxId);
|
||||||
|
|
||||||
|
const createOrRegisterOnExistence = jest
|
||||||
|
.spyOn(register, "createOrRegisterOnExistence")
|
||||||
|
.mockImplementation(() => undefined);
|
||||||
|
|
||||||
|
await register.save();
|
||||||
|
|
||||||
|
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
|
||||||
});
|
});
|
||||||
|
it("new first message from us to multiple recipients should create a room", async () => {
|
||||||
|
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user, users[2].user] });
|
||||||
|
|
||||||
|
const register = new registerMessageInApp(messageId, attrs, boxId);
|
||||||
|
|
||||||
|
const initiateRoom = jest
|
||||||
|
.spyOn(register, "initiateRoom")
|
||||||
|
.mockImplementation(() => undefined);
|
||||||
|
|
||||||
|
await register.save();
|
||||||
|
|
||||||
|
expect(initiateRoom).toHaveBeenCalledWith(ownUser.id, roomType.ROOM);
|
||||||
|
});
|
||||||
|
it("response to new first message to multiple recipients with same members should change room type to GROUP", () => {
|
||||||
|
|
||||||
const register = new registerMessageInApp(1, attrs, 1);
|
});
|
||||||
|
it("response to new first message to multiple recipients with different members should change room type to CHANNEL", () => {
|
||||||
|
|
||||||
const createOrRegisterOnExistence = jest
|
});
|
||||||
.spyOn(register, "createOrRegisterOnExistence")
|
|
||||||
.mockImplementation(() => undefined);
|
|
||||||
|
|
||||||
await register.save();
|
|
||||||
|
|
||||||
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
|
|
||||||
});
|
});
|
||||||
|
describe("from other", () => {
|
||||||
it("DM message from user should be added to DM room", async () => {
|
it("new first message from other to me only should create a room", async () => {
|
||||||
// todo multiple messages
|
const attrs = generateAttrs({ from: [users[1].user], to: [ownUser.user] });
|
||||||
|
|
||||||
|
const register = new registerMessageInApp(messageId, attrs, boxId);
|
||||||
|
|
||||||
|
const createOrRegisterOnExistence = jest
|
||||||
|
.spyOn(register, "createOrRegisterOnExistence")
|
||||||
|
.mockImplementation(() => undefined);
|
||||||
|
|
||||||
|
await register.save();
|
||||||
|
|
||||||
|
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.ROOM);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// it("first GROUP message should create a group", () => {});
|
|
||||||
});
|
});
|
||||||
describe("replies", () => {
|
describe("replies", () => {
|
||||||
it("", () => {});
|
it("", () => {});
|
||||||
|
Loading…
Reference in New Issue
Block a user