change archi and use schema routes
This commit is contained in:
parent
65db4d8b7e
commit
5b6995d6a6
26
back/abl/Account-abl.ts
Normal file
26
back/abl/Account-abl.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { Response } from "express";
|
||||||
|
import { getAccounts, registerAccount } from "../db/api";
|
||||||
|
import { getAddresseId } from "../db/utils/mail";
|
||||||
|
import statusCodes from "../utils/statusCodes";
|
||||||
|
|
||||||
|
export default class Account {
|
||||||
|
static async getAll(body, res: Response) {
|
||||||
|
getAccounts().then((data) => {
|
||||||
|
res.status(statusCodes.OK).json(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static async register(body, res: Response) {
|
||||||
|
const { email, pwd, xoauth, xoauth2, host, port, tls } = body;
|
||||||
|
getAddresseId(email).then((addressId) => {
|
||||||
|
registerAccount(addressId, pwd, xoauth, xoauth2, host, port, tls)
|
||||||
|
.then((mailboxId) => {
|
||||||
|
res.status(statusCodes.OK).json({ id: mailboxId });
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
res.status(statusCodes.INTERNAL_SERVER_ERROR);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// todo change mailbox to account
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
import statusCode from "../utils/statusCodes";
|
import statusCode from "../utils/statusCodes";
|
||||||
import { getRooms } from "../db/api";
|
import { getRooms } from "../db/api";
|
||||||
import logger from "../system/Logger";
|
import logger from "../system/Logger";
|
||||||
|
import { Response } from "express";
|
||||||
|
|
||||||
export async function rooms(body, res) {
|
export async function rooms(body, res: Response) {
|
||||||
const { mailboxId, offset, limit } = body;
|
const { mailboxId, offset, limit } = body;
|
||||||
getRooms(mailboxId).then((rooms) => {
|
getRooms(mailboxId).then((rooms) => {
|
||||||
res.status(statusCode.OK).json(rooms);
|
res.status(statusCode.OK).json(rooms);
|
@ -1,18 +0,0 @@
|
|||||||
import statusCode from "../utils/statusCodes";
|
|
||||||
import { registerAccount } from "../db/api";
|
|
||||||
import { getAddresseId } from "../db/utils/mail";
|
|
||||||
|
|
||||||
export async function addAccount(body, res) {
|
|
||||||
const { email, pwd, xoauth, xoauth2, host, port, tls } = body;
|
|
||||||
getAddresseId(email).then((addressId) => {
|
|
||||||
registerAccount(addressId, pwd, xoauth, xoauth2, host, port, tls)
|
|
||||||
.then((mailboxId) => {
|
|
||||||
res.status(statusCode.OK).json({ id: mailboxId });
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
res.status(statusCode.INTERNAL_SERVER_ERROR);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo change mailbox to account
|
|
@ -36,7 +36,6 @@ export async function createRoom(
|
|||||||
return await execQueryAsyncWithId(query, values);
|
return await execQueryAsyncWithId(query, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo date not good
|
|
||||||
export async function registerMessageInRoom(messageId: number, roomId: number, idate: string | undefined | null) {
|
export async function registerMessageInRoom(messageId: number, roomId: number, idate: string | undefined | null) {
|
||||||
if (!idate) idate = new Date().toString();
|
if (!idate) idate = new Date().toString();
|
||||||
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
|
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
|
||||||
|
@ -77,6 +77,7 @@ export default class RegisterMessageInApp {
|
|||||||
|
|
||||||
async incrementNotSeen(roomId: number) {
|
async incrementNotSeen(roomId: number) {
|
||||||
// todo it appears there is an error with notifications
|
// todo it appears there is an error with notifications
|
||||||
|
console.log("incrementRead", roomId)
|
||||||
if (!this.isSeen) {
|
if (!this.isSeen) {
|
||||||
await incrementNotSeenRoom(roomId);
|
await incrementNotSeenRoom(roomId);
|
||||||
}
|
}
|
||||||
|
@ -2,52 +2,76 @@ import statusCodes from "../utils/statusCodes";
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
import Ajv from "ajv";
|
import { rooms } from "../abl/rooms";
|
||||||
import addFormats from "ajv-formats";
|
import { messages } from "../abl/messages";
|
||||||
const ajv = new Ajv({ allErrors: true });
|
import { members } from "../abl/members";
|
||||||
addFormats(ajv);
|
import {
|
||||||
import schema_account from "../schemas/account_schema.json";
|
validateCreateAccount,
|
||||||
import { addAccount } from "../controllers/addAccount";
|
validateGetAccounts,
|
||||||
import { getAccounts } from "../db/api";
|
validateGetMembers,
|
||||||
import { rooms } from "../controllers/rooms";
|
validateGetMessages,
|
||||||
import { messages } from "../controllers/messages";
|
validateGetRooms,
|
||||||
import { members } from "../controllers/members";
|
} from "../validator/validator";
|
||||||
|
import Account from "../abl/Account-abl";
|
||||||
const validate_account = ajv.compile(schema_account);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all mailboxes and folders for an user
|
* Return all mailboxes and folders for an user
|
||||||
*/
|
*/
|
||||||
router.get("/accounts", (req, res) => {
|
router.get("/accounts", async (req, res) => {
|
||||||
getAccounts().then((data) => {
|
const valid = validateGetAccounts(req.params);
|
||||||
res.status(statusCodes.OK).json(data);
|
if (!valid) {
|
||||||
});
|
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validateGetAccounts.errors });
|
||||||
|
} else {
|
||||||
|
await Account.getAll(req.params, res);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all rooms from a mailbox
|
||||||
|
*/
|
||||||
router.get("/:mailboxId/rooms", async (req, res) => {
|
router.get("/:mailboxId/rooms", async (req, res) => {
|
||||||
// todo use offset limit
|
// todo offet limit
|
||||||
await rooms(req.params, res);
|
const valid = validateGetRooms(req.params);
|
||||||
|
if (!valid) {
|
||||||
|
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validateGetRooms.errors });
|
||||||
|
} else {
|
||||||
|
await rooms(req.params, res);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all messages from a room
|
||||||
|
*/
|
||||||
router.get("/:roomId/messages", async (req, res) => {
|
router.get("/:roomId/messages", async (req, res) => {
|
||||||
const { roomId } = req.params;
|
const valid = validateGetMessages(req.params);
|
||||||
await messages(req.params, res);
|
if (!valid) {
|
||||||
|
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validateGetMessages.errors });
|
||||||
|
} else {
|
||||||
|
await messages(req.params, res);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all members from a room
|
||||||
|
*/
|
||||||
router.get("/:roomId/members", async (req, res) => {
|
router.get("/:roomId/members", async (req, res) => {
|
||||||
const { roomId } = req.params;
|
const valid = validateGetMembers(req.params);
|
||||||
await members(req.params, res);
|
if (!valid) {
|
||||||
|
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validateGetMembers.errors });
|
||||||
|
} else {
|
||||||
|
await members(req.params, res);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a new mailbox inside the app
|
* Register a new mailbox inside the app
|
||||||
*/
|
*/
|
||||||
router.post("/account", async (req, res) => {
|
router.post("/account", async (req, res) => {
|
||||||
const valid = validate_account(req.body);
|
const valid = validateCreateAccount(req.body);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validate_account.errors });
|
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validateCreateAccount.errors });
|
||||||
} else {
|
} else {
|
||||||
await addAccount(req.body, res);
|
await Account.register(req.body, res);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
6
back/validator/schemas/getAccounts-schema.json
Normal file
6
back/validator/schemas/getAccounts-schema.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {},
|
||||||
|
"required": [],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
12
back/validator/schemas/getMembers-schema.json
Normal file
12
back/validator/schemas/getMembers-schema.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"roomId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"roomId"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
12
back/validator/schemas/getMessages-schema.json
Normal file
12
back/validator/schemas/getMessages-schema.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"roomId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"roomId"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
12
back/validator/schemas/getRooms-schema.json
Normal file
12
back/validator/schemas/getRooms-schema.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"mailboxId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"mailboxId"
|
||||||
|
],
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
16
back/validator/validator.ts
Normal file
16
back/validator/validator.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import Ajv from "ajv";
|
||||||
|
import addFormats from "ajv-formats";
|
||||||
|
const ajv = new Ajv({ allErrors: true });
|
||||||
|
addFormats(ajv);
|
||||||
|
|
||||||
|
import createAccountSchema from "./schemas/createAccount-schema.json";
|
||||||
|
import getAccountSchema from "./schemas/getAccounts-schema.json";
|
||||||
|
import getRoomSchema from "./schemas/getRooms-schema.json";
|
||||||
|
import getMessagesSchema from "./schemas/getMessages-schema.json";
|
||||||
|
import getMembersSchema from "./schemas/getMembers-schema.json";
|
||||||
|
|
||||||
|
export const validateCreateAccount = ajv.compile(createAccountSchema);
|
||||||
|
export const validateGetAccounts = ajv.compile(getAccountSchema);
|
||||||
|
export const validateGetRooms = ajv.compile(getRoomSchema);
|
||||||
|
export const validateGetMessages = ajv.compile(getMessagesSchema);
|
||||||
|
export const validateGetMembers = ajv.compile(getMembersSchema);
|
Loading…
Reference in New Issue
Block a user