init

parents
Pipeline #10361 passed with stages
in 9 minutes and 51 seconds
APP_PORT=3000
API_SECRET=test123
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=courier
DB_NAME=courier
DB_PASSWORD=test123
node_modules/
-- MySQL dump 10.13 Distrib 5.7.26, for Linux (x86_64)
--
-- Host: localhost Database: courier
-- ------------------------------------------------------
-- Server version 5.7.26-0ubuntu0.16.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `counteragents`
--
DROP TABLE IF EXISTS `counteragents`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `counteragents` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` enum('сотрудник','курьер','клиент','акционер','прочий контрагент') COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `transactionId` (`transactionId`),
CONSTRAINT `counteragents_ibfk_1` FOREIGN KEY (`transactionId`) REFERENCES `transactions` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `counteragents`
--
LOCK TABLES `counteragents` WRITE;
/*!40000 ALTER TABLE `counteragents` DISABLE KEYS */;
INSERT INTO `counteragents` VALUES (1,'сотрудник','Иванов Иван Иванович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(2,'сотрудник','Петров Пётр Петрович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(3,'курьер','Абдулов Абдула Абдулович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(4,'курьер','Ахмедов Ахмед Ахмедович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(5,'акционер','Абрамович Абрам Абрамович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(6,'акционер','Усманов Усман Усманович','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(7,'клиент','ООО Рога','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(8,'клиент','ООО Копыта','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(9,'прочий контрагент','ООО «Доставка кальянов в офис»','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(10,'прочий контрагент','ООО «Доставка шлюх в офис, чтоб никто не знал»','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL);
/*!40000 ALTER TABLE `counteragents` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `transactions`
--
DROP TABLE IF EXISTS `transactions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` enum('расход','приход') COLLATE utf8_unicode_ci NOT NULL,
`article` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `transactions`
--
LOCK TABLES `transactions` WRITE;
/*!40000 ALTER TABLE `transactions` DISABLE KEYS */;
/*!40000 ALTER TABLE `transactions` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`phone` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `phone` (`phone`),
UNIQUE KEY `password` (`password`),
KEY `transactionId` (`transactionId`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`transactionId`) REFERENCES `transactions` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `users`
--
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (1,'+79260000001','Феликсов Феликс Феликсович','jhfg8293gi','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(2,'+79260000002','Абрамович Абрам Абрамович','jv9EWHgOI2l','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(3,'+79260000003','Иванов Иван Иванович','fhj8293gh29ui','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL);
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `wallets`
--
DROP TABLE IF EXISTS `wallets`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `wallets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `transactionId` (`transactionId`),
CONSTRAINT `wallets_ibfk_1` FOREIGN KEY (`transactionId`) REFERENCES `transactions` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `wallets`
--
LOCK TABLES `wallets` WRITE;
/*!40000 ALTER TABLE `wallets` DISABLE KEYS */;
INSERT INTO `wallets` VALUES (1,'Альфа Банк','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(2,'Тинькофф','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(3,'Касса #1','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL),(4,'Левый карман','2019-07-09 10:09:15','2019-07-09 10:09:15',NULL);
/*!40000 ALTER TABLE `wallets` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2019-07-09 13:34:18
require('dotenv').config();
const fastify = require('fastify')({ logger: true });
const models = require('./models');
const messages = require('./messages');
fastify.register(require('fastify-formbody'));
fastify.register(require('fastify-jwt'), {
secret: process.env.API_SECRET
});
fastify.decorate('authenticate', async function(request, reply) {
try {
await request.jwtVerify();
} catch (err) {
reply.send(err);
}
});
fastify.post('/signup', (request, reply) => {
const { phone, password } = request.body;
models.User.findOne({ where: { phone, password } })
.then(user => {
request.log.info(`TAPIOKA! ${user}`)
if (user) {
const token = fastify.jwt.sign({ user });
reply.send({ result: 'ok', token });
} else {
reply.send({ result: 'error', msg: messages.MSG_USER_NOT_FOUND });
}
});
});
const preValidation = [fastify.authenticate];
fastify.get('/transactions', { preValidation }, async (request, reply) => {
models.Transaction.findAll().then(transactions => {
reply.send({ result: 'ok', data: transactions });
});
});
fastify.post('/add_transaction', { preValidation }, async (request, reply) => {
const transaction = request.body;
models.Transaction.create(transaction).then(result => {
reply.send({ result: 'ok' });
});
});
fastify.get('/counteragents', { preValidation }, async (request, reply) => {
models.Counteragent.findAll({ attributes: ['id', 'type', 'name'] }).then(counteragents => {
reply.send({ result: 'ok', data: counteragents });
});
});
fastify.get('/wallets', { preValidation }, async (request, reply) => {
models.Wallet.findAll({ attributes: ['id', 'name'] }).then(wallets => {
reply.send({ result: 'ok', data: wallets });
});
});
const start = async () => {
try {
await fastify.listen(process.env.APP_PORT || 3000);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
}
start();
MSG_USER_NOT_FOUND = 'Не удалось войти с указанными данными. Проверьте правильность логина и пароля.';
module.exports = {
MSG_USER_NOT_FOUND
};
require('dotenv').config();
const Sequelize = require('sequelize');
const Model = Sequelize.Model;
const sequelize = new Sequelize(
process.env.DB_DATABASE, process.env.DB_NAME, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
dialect: 'mysql'
});
class User extends Model {}
User.init({
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
phone: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false,
unique: true
}
}, {
sequelize,
modelName: 'user'
});
class Wallet extends Model {}
Wallet.init({
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'wallet'
});
class Counteragent extends Model {}
Counteragent.init({
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
type: {
type: Sequelize.ENUM('сотрудник', 'курьер', 'клиент','акционер','прочий контрагент'),
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'counteragent'
});
class Transaction extends Model {}
Transaction.init({
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
type: {
type: Sequelize.ENUM('расход', 'приход'),
allowNull: false
},
article: {
type: Sequelize.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'transaction'
});
Transaction.hasOne(User);
Transaction.hasOne(Wallet);
Transaction.hasOne(Counteragent);
sequelize.sync()
module.exports = {
User, Wallet, Counteragent, Transaction
};
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "https://git.ivolga.tech:444/Kuchaev/courier-test-backend.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^8.0.0",
"fastify": "^2.6.0",
"fastify-formbody": "^3.1.0",
"fastify-jwt": "^1.0.0",
"mysql2": "^1.6.5",
"sequelize": "^5.9.4"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment