conf(wai): eslint

dev/supplier-email-drawer
lyt 9 months ago
parent 086683f782
commit 3158595b30

@ -1,18 +1,29 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true,
'jest/globals': true,
node: true,
es2021: true,
},
extends: ['standard', 'plugin:prettier/recommended'],
plugins: ['prettier', 'jest'],
extends: ['standard', 'prettier', 'plugin:prettier/recommended'],
plugins: ['n', 'prettier'],
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
// Add here all the extra rules based on the developer preferences
'no-unused-vars': ['warn', { args: 'after-used', vars: 'all' }],
'prettier/prettier': ['warn', { parser: 'flow' }],
'n/exports-style': ['warn', 'module.exports'],
'n/file-extension-in-import': ['error', 'always'],
'n/prefer-promises/dns': 'error',
'n/prefer-promises/fs': 'error',
'no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
'no-console': ['warn', { allow: ['warn', 'error'] }],
},
overrides: [
{
files: ['test/**/*.js'],
env: {
mocha: true,
},
},
],
};

@ -0,0 +1 @@
* text=auto eol=lf

@ -5,5 +5,7 @@
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"proseWrap": "always"
"proseWrap": "always",
"arrowParens": "avoid",
"endOfLine": "auto"
}

@ -3,10 +3,10 @@
const { sessionStore } = require('../../core'); // Import from core/index.js
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
const { getConnection } = require('../../services/connections.service');
const { objectMapper, isEmpty } = require('../../utils/commons.util');
const { domain, name: domainName } = require('../../config').server;
const { isEmpty } = require('../../utils/commons.util');
const { domain } = require('../../config').server;
exports.newConnect = async ctx => {
const newConnect = async ctx => {
const { phone } = ctx.query;
const existsSession = sessionStore.getSession(phone);
if (!isEmpty(existsSession)) {
@ -25,11 +25,17 @@ exports.newConnect = async ctx => {
}
};
exports.getAll = async () => {
const getAll = async () => {
const findConnection = await getConnection({});
return findConnection;
};
exports.getSessions = async ctx => {
const getSessions = async () => {
return Array.from(sessionStore.sessions);
};
module.exports = {
newConnect,
getAll,
getSessions,
};

@ -15,7 +15,7 @@ function sleep(ms) {
/**
* @deprecated 即将废弃
*/
exports.sendText = async ctx => {
const sendText = async ctx => {
const { from, to, msgcontent, content: _content, actionId } = ctx.request.body;
const content = _content || msgcontent.body || '';
if (!from || !content) {
@ -54,7 +54,7 @@ exports.sendText = async ctx => {
}
};
exports.send = async ctx => {
const send = async ctx => {
const { msgtype, from, to, msgcontent, content: _content, actionId } = ctx.request.body;
const content = _content || msgcontent.body || '';
logger.info('send', msgtype, from, to, content);
@ -81,3 +81,8 @@ exports.send = async ctx => {
ctx.assert(null, 500, 'Failed to send message');
}
};
module.exports = {
send,
sendText,
};

@ -4,13 +4,13 @@ const generateId = require('../../utils/generateId.util');
const { domain, name: domainName } = require('../../config').server;
const whatsappEvents = require('../emitter');
const { callWebhook } = require('../webhook');
const { addConnection, updateConnection, addCurrentConnection, resetConnection } = require('../../services/connections.service');
const { updateConnection, addCurrentConnection, resetConnection } = require('../../services/connections.service');
const { objectMapper, pick } = require('../../utils/commons.util');
const { sessionStore } = require('..');
const { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
const logger = require('../../utils/logger.util');
const { DbData, } = require('../../helper/wai.msg.helper');
const { DbData } = require('../../helper/wai.msg.helper');
const connectionEventNames = ['connection:connect', 'connection:open', 'connection:close'];
const messageEventNames = ['message:received', 'message:updated'];
@ -25,13 +25,13 @@ const statusMapped = { saved: 'accepted', pending: 'accepted', sent: 'sent', del
const directionField = { 'message:received': 'inbound', 'message:updated': 'outbound' };
const directionPrefix = { inbound: 'in_', outbound: 'out_' };
const uniqueMsgId = (msg) => `${directionPrefix[msg.direction]}${msg.to.replace('+', '')}_${msg.id}`;
const uniqueMsgId = msg => (msg.id && msg.direction ? `${directionPrefix[msg.direction]}${msg.to.replace('+', '')}_${msg.id}` : undefined);
/**
* @returns {Object} webhookBody
*/
const webhookBodyBuilder = (messageData, messageType) => {
const defaultContent = { id: '', from: '', to: '', externalId: '', type: '', direction: '', status: '', };
const defaultContent = { id: '', from: '', to: '', externalId: '', type: '', direction: '', status: '' };
const message = {
id: `evt_${generateId().replace(/-/g, '')}`,
type: eventTypeMapped[messageType],
@ -45,7 +45,7 @@ const webhookBodyBuilder = (messageData, messageType) => {
...defaultContent,
...messageData,
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
id: messageData.id && messageData.direction ? uniqueMsgId(messageData) : (messageData.id || generateId()),
id: uniqueMsgId(messageData) || messageData.id || generateId(),
wamid: messageData.id || '',
// direction: directionField[messageType],
status: statusMapped?.[messageData.status] || messageData.status || '',

@ -253,7 +253,7 @@ const waiMsgTypeMapped = {
/**
* API Payload to Send
*/
exports.ctxToSendBuilder = ctxContent => {
const ctxToSendBuilder = ctxContent => {
const { contentToSend } = waiMsgTypeMapped[ctxContent.msgtype];
const msgReady = contentToSend(ctxContent);
return msgReady;
@ -262,7 +262,7 @@ exports.ctxToSendBuilder = ctxContent => {
/**
* API Payload to DB
*/
exports.ctxToDB = ctxContent => {
const ctxToDB = ctxContent => {
const { dataToDB } = waiMsgTypeMapped[ctxContent.msgtype];
const msgReady = dataToDB(ctxContent);
return msgReady;
@ -271,8 +271,14 @@ exports.ctxToDB = ctxContent => {
/**
* Parse DB Data to UI/API/Webhook
*/
exports.DbData = row => {
const DbData = row => {
const { DbData } = waiMsgTypeMapped[row.msgtype];
const msgReady = DbData(row);
return msgReady;
};
module.exports = {
ctxToSendBuilder,
ctxToDB,
DbData,
};

File diff suppressed because it is too large Load Diff

@ -12,18 +12,16 @@
"test": "NODE_ENV=test jest'"
},
"devDependencies": {
"eslint": "5.16.0",
"eslint-config-prettier": "4.3.0",
"eslint-config-standard": "12.0.0",
"eslint-plugin-import": "2.17.2",
"eslint-plugin-jest": "22.5.1",
"eslint-plugin-node": "9.0.1",
"eslint-plugin-prettier": "3.1.0",
"eslint-plugin-promise": "4.1.1",
"eslint-plugin-standard": "4.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-promise": "^6.6.0",
"husky": "2.3.0",
"nodemon": "1.19.0",
"prettier": "1.17.1"
"prettier": "^3.4.2"
},
"dependencies": {
"@koa/cors": "2.2.3",

@ -17,10 +17,10 @@ const getOutboundMessage = async msg => {
return r?.toJSON() || {};
};
const createOutboundMessage = async (data) => {
const createOutboundMessage = async data => {
const r = await OutboundModelModel.create(data);
return r;
}
};
/**
*
@ -37,7 +37,7 @@ const upsertOutboundMessage = async (data, where = {}) => {
const _where = isEmpty(where) ? data : where;
const [instance, created] = await OutboundModelModel.findOrCreate({ where: _where, defaults: { ...data } });
if (!created) {
await instance.update({ ...data, }, { where });
await instance.update({ ...data }, { where });
const savedI = await instance.save(); // reload
console.info('update OutboundMessage --- 2\n', savedI.toJSON());
return savedI.toJSON();

Loading…
Cancel
Save