Skip to content

Commit

Permalink
SAMA-46 correcting the names of the message_status & file collections (
Browse files Browse the repository at this point in the history
…#75)

* SAMA-46 add migration for renaming collections

* SAMA-46 fiexd test, rename PushEvent model

* SAMA-46 add migration: drop old_users collection
  • Loading branch information
Oleksandr1414 authored Oct 19, 2023
1 parent e5f3f6d commit b71eb08
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class File extends BaseModel {
}

static get collection() {
return "file";
return "files";
}

static get visibleFields() {
Expand Down
2 changes: 1 addition & 1 deletion app/models/message_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class MessageStatus extends BaseModel {
}

static get collection() {
return "message_status";
return "message_statuses";
}

static get visibleFields() {
Expand Down
2 changes: 1 addition & 1 deletion app/models/push_event.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BaseModel from "./base/base.js";

export default class PushEvents extends BaseModel {
export default class PushEvent extends BaseModel {
constructor(params) {
super(params);
}
Expand Down
4 changes: 2 additions & 2 deletions app/repositories/push_notifications_repository.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseRepository from "./base.js";
import PushEvents from "../models/push_event.js";
import PushEvent from "../models/push_event.js";
import PushSubscription from "../models/push_subscription.js";
import SessionRepository from "./session_repository.js";
import { ACTIVE } from "../store/session.js";
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class PushNotificationsRepository extends BaseRepository {
message: JSON.stringify(pushMessage),
};

const pushEvent = new PushEvents(pushEventParams);
const pushEvent = new PushEvent(pushEventParams);
await pushEvent.save();

await this.sendPushNotification(recipients_ids, pushMessage);
Expand Down
39 changes: 39 additions & 0 deletions migrations/20231019132828-rename-file-message-status-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const up = async (db, client) => {
// vv CREATE MESSAGE STATUSES vv //
let indexes = await db.collection("message_status").indexes();
indexes = indexes.filter(({ name }) => name !== "_id_");

indexes = indexes.map((index) => {
const { v, key, ...options } = index;
return { key, options };
});

const newCollection = await db.createCollection("message_statuses");
for (const { key, options } of indexes) {
await newCollection.createIndex(key, options);
}

// vv COPY MESSAGE STATUSES vv //
await db
.collection("message_status")
.aggregate([{ $out: "message_statuses" }])
.toArray();

// vv DROP MESSAGE STATUSES vv //
await db.collection("message_status").drop();

// vv FILES vv //
await db.createCollection("files");
await db
.collection("file")
.aggregate([{ $out: "files" }])
.toArray();
await db.collection("file").drop();
};

export const down = async (db, client) => {
await db
.collection("message_statuses")
.rename("message_status", { dropTarget: true });
await db.collection("files").rename("file", { dropTarget: true });
};
7 changes: 7 additions & 0 deletions migrations/20231019143244-drop-old-users-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const up = async (db, client) => {
await db.collection("old_users").drop();
};

export const down = async (db, client) => {
await db.createCollection("old_users");
};
17 changes: 9 additions & 8 deletions test/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,21 +608,21 @@ describe("Contacts functions", async () => {
first_name: "Name11_6",
last_name: "Surname11_6",
company: "UserCompany",
email: [{ type: "work", value: "test_matched_6_email" }],
email: [{ type: "work", value: "[email protected]" }],
phone: [{ type: "home", value: "test_6_phone" }],
},
{
first_name: "Name22_6",
last_name: "Surname22_6",
company: "UserCompany",
email: [{ type: "work", value: "test_matched_6_email" }],
email: [{ type: "work", value: "[email protected]" }],
phone: [{ type: "home", value: "test_6_phone" }],
},
{
first_name: "Name33_6",
last_name: "Surname33_6",
company: "UserCompany",
email: [{ type: "work", value: "test_matched_6_email" }],
email: [{ type: "work", value: "[email protected]" }],
phone: [{ type: "home", value: "test_6_phone" }],
},
],
Expand All @@ -641,7 +641,7 @@ describe("Contacts functions", async () => {
requestData = {
request: {
user_edit: {
email: "test_matched_6_email",
email: "[email protected]",
phone: "test_6_phone",
},
id: "1",
Expand All @@ -666,6 +666,7 @@ describe("Contacts functions", async () => {
mockedWS,
requestData
);
console.log(responseData.response.contacts[0]);

assert.strictEqual(requestData.request.id, responseData.response.id);
assert.strictEqual(
Expand Down Expand Up @@ -702,7 +703,7 @@ describe("Contacts functions", async () => {
let requestData = {
request: {
user_edit: {
email: "test_matched_7_email",
email: "[email protected]",
},
id: "1",
},
Expand Down Expand Up @@ -730,15 +731,15 @@ describe("Contacts functions", async () => {
assert.strictEqual(requestData.request.id, responseData.response.id);
assert.strictEqual(
responseData.response.contacts[0].email[0].value,
"test_matched_7_email"
"[email protected]"
);
assert.strictEqual(
responseData.response.contacts[1].email[0].value,
"test_matched_7_email"
"[email protected]"
);
assert.strictEqual(
responseData.response.contacts[2].email[0].value,
"test_matched_7_email"
"[email protected]"
);
assert.strictEqual(
responseData.response.contacts[0].email[0].matched_user_id,
Expand Down
4 changes: 2 additions & 2 deletions test/push_notifications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PushEvents from "../app/models/push_event.js";
import PushEvent from "../app/models/push_event.js";
import PushSubscription from "../app/models/push_subscription.js";
import User from "./../app/models/user.js";
import assert from "assert";
Expand Down Expand Up @@ -574,7 +574,7 @@ describe("PushNotification functions", async () => {
after(async () => {
await User.clearCollection();
await PushSubscription.clearCollection();
await PushEvents.clearCollection();
await PushEvent.clearCollection();
usersIds = [];
});
});
4 changes: 2 additions & 2 deletions test/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ describe("User cycle", async () => {
user_create: {
login: "login_12345",
password: "new_pasw31",
email: "copy_email",
email: "[email protected]",
phone: "copy_phone",
deviceId: "pc",
},
Expand Down Expand Up @@ -449,7 +449,7 @@ describe("User cycle", async () => {
const requestData = {
request: {
user_edit: {
email: "copy_email",
email: "[email protected]",
},
id: "5_1",
},
Expand Down

0 comments on commit b71eb08

Please sign in to comment.