Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert cjs modules to esm #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions chat-messaging/resources/chatMessages/chatMessages.schema.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
const Joi = require('joi');

module.exports = Joi.object({
import Joi from "joi";
export default Joi.object({
_id: Joi.string(),
id: Joi.string().allow(null).allow(''),
id: Joi.string().allow(null).allow(""),
createdOn: Joi.date(),
updatedOn: Joi.date(),

attachments: Joi.array()
.items(
Joi.object({
url: Joi.string().allow(null).allow(''),
})
url: Joi.string().allow(null).allow(""),
}),
)
.allow(null),
messageHTML: Joi.string().allow(null).allow(''),

messageHTML: Joi.string().allow(null).allow(""),
chatRoom: Joi.object({
_id: Joi.string(),
}).allow(null),
fromUser: Joi.object({
_id: Joi.string(),
avatarUrl: Joi.string().allow(null).allow(''),
fullName: Joi.string().allow(null).allow(''),
avatarUrl: Joi.string().allow(null).allow(""),
fullName: Joi.string().allow(null).allow(""),
}).allow(null),

metadata: Joi.object({}).allow(null),

parentMessage: Joi.object({
_id: Joi.string(),
}).allow(null),
childMessagesCount: Joi.number(),
});
});
51 changes: 24 additions & 27 deletions chat-messaging/resources/chatMessages/endpoints/create.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
const Joi = require('joi');

const chatMessageService = require('db').services.chatMessages;

module.exports.handler = async (ctx) => {
const { messageHTML, attachments, chatRoomId, id, parentMessageId } = ctx.validatedData;

import Joi from "joi";
import db from "db";
import attachUser from "middlewares/attachUser";
import shouldExist from "middlewares/shouldExist";
const chatMessageService = db.services.chatMessages;
export const handler = async (ctx) => {
const { messageHTML, attachments, chatRoomId, id, parentMessageId } =
ctx.validatedData;
const chatMessage = await chatMessageService.create({
id,
chatRoom: { _id: chatRoomId },
fromUser: { _id: ctx.state.user._id },
messageHTML,
attachments,
...(parentMessageId ? { parentMessage: { _id: parentMessageId }} : { })
...(parentMessageId ? { parentMessage: { _id: parentMessageId } } : {}),
});

ctx.body = chatMessage;
};

module.exports.middlewares = [
require('middlewares/attachUser'),
require('middlewares/shouldExist')('chatRooms', {
export const middlewares = [
attachUser,
shouldExist("chatRooms", {
criteria: (ctx) => {
return {
_id: ctx.request.body.chatRoomId,
'users._id': ctx.state.user._id
"users._id": ctx.state.user._id,
};
},
}),
];

module.exports.endpoint = {
url: '/',
method: 'post',
export const endpoint = {
url: "/",
method: "post",
};

module.exports.requestSchema = Joi.object({
export const requestSchema = Joi.object({
attachments: Joi.array()
.items(
Joi.object({
url: Joi.string().allow(null).allow(''),
})
url: Joi.string().allow(null).allow(""),
}),
)
.allow(null),
messageHTML: Joi.string().allow(null).allow(''),
chatRoomId: Joi.string().allow(null).allow(''),
id: Joi.string().allow(null).allow(''),
parentMessageId: Joi.string().allow(null).allow(''),
});
messageHTML: Joi.string().allow(null).allow(""),
chatRoomId: Joi.string().allow(null).allow(""),
id: Joi.string().allow(null).allow(""),
parentMessageId: Joi.string().allow(null).allow(""),
});
48 changes: 19 additions & 29 deletions chat-messaging/resources/chatMessages/endpoints/list.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const Joi = require('joi');

const chatMessageService = require('db').services.chatMessages;
const chatRoomService = require('db').services.chatRooms;

module.exports.handler = async (ctx) => {
import Joi from "joi";
import db from "db";
import isAuthorized from "middlewares/isAuthorized";
const chatMessageService = db.services.chatMessages;
const chatRoomService = db.services.chatRooms;
export const handler = async (ctx) => {
const {
page,
perPage,
sort = '-createdOn',
sort = "-createdOn",
beforeDate,
chatRoomId,
} = ctx.validatedData;

let chatRoom = await chatRoomService.findOne({
_id: ctx.request.query.chatRoomId,
'users._id': ctx.state.user._id,
"users._id": ctx.state.user._id,
});

if (!chatRoom) {
ctx.body = {
isNoChatRoom: true,
Expand All @@ -26,33 +24,25 @@ module.exports.handler = async (ctx) => {
};
return;
}

const { results, pagesCount, count } = await chatMessageService.find(
{
...(beforeDate ? { createdOn: { $lte: beforeDate } } : {}),

'chatRoom._id': chatRoomId,
"chatRoom._id": chatRoomId,
},
{ page, perPage, sort }
{ page, perPage, sort },
);

ctx.body = { results, pagesCount, count };
};

module.exports.middlewares = [
require('middlewares/isAuthorized'),
];

module.exports.endpoint = {
url: '/',
method: 'get',
export const middlewares = [isAuthorized];
export const endpoint = {
url: "/",
method: "get",
};

module.exports.requestSchema = Joi.object({
export const requestSchema = Joi.object({
page: Joi.number(),
perPage: Joi.number(),
sort: Joi.string().allow(null).allow(''),
afterDate: Joi.string().allow(null).allow(''),
sort: Joi.string().allow(null).allow(""),
afterDate: Joi.string().allow(null).allow(""),
beforeDate: Joi.date(),
chatRoomId: Joi.string().allow(null).allow(''),
});
chatRoomId: Joi.string().allow(null).allow(""),
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ioEmitter = require('ioEmitter');
const chatMessageService = require('db').services.chatMessages;

chatMessageService.on('created', async ({ doc: chatMessage }) => {
import ioEmitter from "ioEmitter";
import db from "db";
const chatMessageService = db.services.chatMessages;
chatMessageService.on("created", async ({ doc: chatMessage }) => {
ioEmitter
.to(`chatRoom-${chatMessage.chatRoom._id}`)
.emit('chatMessage:created', {
.emit("chatMessage:created", {
chatMessage,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const chatMessageService = require('db').services.chatMessages;

import db from "db";
const chatMessageService = db.services.chatMessages;
let updateChildMessagesCount = async ({ doc: chatMessage }) => {
if (chatMessage.parentMessage) {
await chatMessageService.atomic.update(
{ _id: chatMessage.parentMessage._id }, {
{ _id: chatMessage.parentMessage._id },
{
$set: {
childMessagesCount: await chatMessageService.count({'parentMessage._id': chatMessage.parentMessage._id})
}
}
childMessagesCount: await chatMessageService.count({
"parentMessage._id": chatMessage.parentMessage._id,
}),
},
},
);
}
}

chatMessageService.on('created', updateChildMessagesCount);
chatMessageService.on('removed', updateChildMessagesCount);
};
chatMessageService.on("created", updateChildMessagesCount);
chatMessageService.on("removed", updateChildMessagesCount);
13 changes: 5 additions & 8 deletions chat-messaging/resources/chatRooms/chatRooms.schema.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
const Joi = require('joi');

module.exports = Joi.object({
import Joi from "joi";
export default Joi.object({
_id: Joi.string(),
createdOn: Joi.date(),
updatedOn: Joi.date(),

users: Joi.array()
.items(
Joi.object({
_id: Joi.string(),
}).allow(null)
}).allow(null),
)
.allow(null),

lastMessageHTML: Joi.string().allow(null).allow(''),
lastMessageHTML: Joi.string().allow(null).allow(""),
lastMessageUser: Joi.object({
_id: Joi.string(),
}).allow(null),
lastMessageSentOn: Joi.date(),
});
});
29 changes: 13 additions & 16 deletions chat-messaging/resources/chatRooms/handlers/updateLastMessage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const chatMessageService = require('db').services.chatMessages;
const chatRoomService = require('db').services.chatRooms;

chatMessageService.on('created', async ({ doc: message }) => {
await chatRoomService.updateOne(
{ _id: message.chatRoom._id },
(chatRoom) => {
return {
...chatRoom,
lastMessageHTML: message.messageHTML,
lastMessageUser: message.fromUser || null,
lastMessageSentOn: message.createdOn,
};
}
);
});
import db from "db";
const chatMessageService = db.services.chatMessages;
const chatRoomService = db.services.chatRooms;
chatMessageService.on("created", async ({ doc: message }) => {
await chatRoomService.updateOne({ _id: message.chatRoom._id }, (chatRoom) => {
return {
...chatRoom,
lastMessageHTML: message.messageHTML,
lastMessageUser: message.fromUser || null,
lastMessageSentOn: message.createdOn,
};
});
});
28 changes: 16 additions & 12 deletions file-upload/app-config/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const assertEnv = require('app-config/assertEnv');

assertEnv(['STORAGE_ENDPOINT', 'STORAGE_ACCESS_KEY', 'STORAGE_SECRET_ACCESS_KEY', 'STORAGE_BUCKET']);

module.exports = {
cloudStorage: {
endpoint: process.env.STORAGE_ENDPOINT,
accessKeyId: process.env.STORAGE_ACCESS_KEY,
secretAccessKey: process.env.STORAGE_SECRET_ACCESS_KEY,
bucket: process.env.STORAGE_BUCKET,
},
};
import assertEnv from "app-config/assertEnv";
assertEnv([
"STORAGE_ENDPOINT",
"STORAGE_ACCESS_KEY",
"STORAGE_SECRET_ACCESS_KEY",
"STORAGE_BUCKET",
]);
export const cloudStorage = {
endpoint: process.env.STORAGE_ENDPOINT,
accessKeyId: process.env.STORAGE_ACCESS_KEY,
secretAccessKey: process.env.STORAGE_SECRET_ACCESS_KEY,
bucket: process.env.STORAGE_BUCKET,
};
export default {
cloudStorage,
};
26 changes: 10 additions & 16 deletions file-upload/resources/files/endpoints/upload.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
const Joi = require('joi');

const isAuthorized = require('middlewares/isAuthorized');
const uploadFile = require('middlewares/uploadFile');

const uploadAndStoreFile = require('resources/files/methods/uploadAndStoreFile');

module.exports.handler = async (ctx) => {
import Joi from "joi";
import isAuthorized from "middlewares/isAuthorized";
import uploadFile from "middlewares/uploadFile";
import uploadAndStoreFile from "resources/files/methods/uploadAndStoreFile";
export const handler = async (ctx) => {
const { file } = ctx.request;
let createdFile = await uploadAndStoreFile({
file,
userId: ctx.state.user._id,
});
ctx.body = createdFile;
};

module.exports.middlewares = [isAuthorized, uploadFile.single('file')];

module.exports.endpoint = {
url: '/',
method: 'post',
export const middlewares = [isAuthorized, uploadFile.single("file")];
export const endpoint = {
url: "/",
method: "post",
};

module.exports.requestSchema = Joi.object({});
export const requestSchema = Joi.object({});
13 changes: 6 additions & 7 deletions file-upload/resources/files/files.schema.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const Joi = require('joi');

module.exports = Joi.object({
import Joi from "joi";
export default Joi.object({
_id: Joi.string(),
createdOn: Joi.date(),
updatedOn: Joi.date(),
name: Joi.string().allow(null).allow(''),
url: Joi.string().allow(null).allow(''),
name: Joi.string().allow(null).allow(""),
url: Joi.string().allow(null).allow(""),
size: Joi.object({}).allow(null),
customerId: Joi.string().allow(null).allow(''),
customerId: Joi.string().allow(null).allow(""),
creator: Joi.object({
_id: Joi.string(),
}).allow(null),
customer: Joi.object({
_id: Joi.string(),
}).allow(null),
});
});
Loading