Frontend integration fixes

parent 2737898a
Pipeline #10363 passed with stages
in 13 minutes and 14 seconds
Параметры для базы данных находятся в .env.
Для запуска - 'npm run start'.
......@@ -28,10 +28,7 @@ CREATE TABLE `counteragents` (
`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
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
......@@ -41,7 +38,7 @@ CREATE 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);
INSERT INTO `counteragents` VALUES (1,'сотрудник','Иванов Иван Иванович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(2,'сотрудник','Петров Пётр Петрович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(3,'курьер','Абдулов Абдула Абдулович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(4,'курьер','Ахмедов Ахмед Ахмедович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(5,'акционер','Абрамович Абрам Абрамович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(6,'акционер','Усманов Усман Усманович','2019-07-09 10:09:15','2019-07-09 10:09:15'),(7,'клиент','ООО Рога','2019-07-09 10:09:15','2019-07-09 10:09:15'),(8,'клиент','ООО Копыта','2019-07-09 10:09:15','2019-07-09 10:09:15'),(9,'прочий контрагент','ООО «Доставка кальянов в офис»','2019-07-09 10:09:15','2019-07-09 10:09:15'),(10,'прочий контрагент','ООО «Доставка шлюх в офис, чтоб никто не знал»','2019-07-09 10:09:15','2019-07-09 10:09:15');
/*!40000 ALTER TABLE `counteragents` ENABLE KEYS */;
UNLOCK TABLES;
......@@ -56,10 +53,20 @@ 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,
`sum` int(11) NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
`userId` int(11) DEFAULT NULL,
`counteragentId` int(11) DEFAULT NULL,
`walletId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `counteragentId` (`counteragentId`),
KEY `walletId` (`walletId`),
CONSTRAINT `transactions_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `transactions_ibfk_2` FOREIGN KEY (`counteragentId`) REFERENCES `counteragents` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `transactions_ibfk_3` FOREIGN KEY (`walletId`) REFERENCES `wallets` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
......@@ -85,12 +92,9 @@ CREATE TABLE `users` (
`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
UNIQUE KEY `password` (`password`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
......@@ -100,7 +104,7 @@ CREATE 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);
INSERT INTO `users` VALUES (1,'+79260000001','Феликсов Феликс Феликсович','jhfg8293gi','2019-07-09 10:09:15','2019-07-09 10:09:15'),(2,'+79260000002','Абрамович Абрам Абрамович','jv9EWHgOI2l','2019-07-09 10:09:15','2019-07-09 10:09:15'),(3,'+79260000003','Иванов Иван Иванович','fhj8293gh29ui','2019-07-09 10:09:15','2019-07-09 10:09:15');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
......@@ -116,10 +120,7 @@ CREATE TABLE `wallets` (
`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
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
......@@ -129,7 +130,7 @@ CREATE 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);
INSERT INTO `wallets` VALUES (1,'Альфа Банк','2019-07-09 10:09:15','2019-07-09 10:09:15'),(2,'Тинькофф','2019-07-09 10:09:15','2019-07-09 10:09:15'),(3,'Касса #1','2019-07-09 10:09:15','2019-07-09 10:09:15'),(4,'Левый карман','2019-07-09 10:09:15','2019-07-09 10:09:15');
/*!40000 ALTER TABLE `wallets` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
......@@ -142,4 +143,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2019-07-09 13:34:18
-- Dump completed on 2019-07-11 11:43:01
......@@ -2,6 +2,9 @@ require('dotenv').config();
const fastify = require('fastify')({ logger: true });
const models = require('./models');
const messages = require('./messages');
const cors = require('cors');
fastify.use(cors());
fastify.register(require('fastify-formbody'));
fastify.register(require('fastify-jwt'), {
......@@ -18,11 +21,12 @@ fastify.decorate('authenticate', async function(request, reply) {
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 });
......@@ -33,22 +37,43 @@ fastify.post('/signup', (request, reply) => {
const preValidation = [fastify.authenticate];
fastify.get('/transactions', { preValidation }, async (request, reply) => {
models.Transaction.findAll().then(transactions => {
models.Transaction.findAll({
include: [ models.User, models.Wallet, models.Counteragent]
}).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' });
const auth = request.headers.authorization;
const token = auth.split(' ')[1];
fastify.jwt.verify(token, (err, decoded) => {
const transaction = request.body;
transaction.userId = decoded.user.id;
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 });
});
const { type } = request.query;
if (type) {
models.Counteragent.findAll({
attributes: ['id', 'type', 'name'],
where: { type }
}).then(counteragents => {
reply.send({ result: 'ok', data: counteragents });
});
} else {
models.Counteragent.findAll({ attributes: ['id', 'type', 'name'] })
.then(counteragents => {
reply.send({ result: 'ok', data: counteragents });
});
}
});
fastify.get('/wallets', { preValidation }, async (request, reply) => {
......
......@@ -88,17 +88,24 @@ Transaction.init({
article: {
type: Sequelize.STRING,
allowNull: false
},
sum: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
sequelize,
modelName: 'transaction'
});
Transaction.hasOne(User);
Transaction.hasOne(Wallet);
Transaction.hasOne(Counteragent);
User.hasMany(Transaction, { foreignKey: 'userId' });
Counteragent.hasMany(Transaction, { foreignKey: 'counteragentId' });
Wallet.hasMany(Transaction, { foreignKey: 'walletId' });
Transaction.belongsTo(User, { foreignKey: 'userId' });
Transaction.belongsTo(Counteragent, { foreignKey: 'counteragentId' });
Transaction.belongsTo(Wallet, { foreignKey: 'walletId' });
sequelize.sync()
sequelize.sync();
module.exports = {
User, Wallet, Counteragent, Transaction
......
This diff is collapsed.
......@@ -13,6 +13,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"fastify": "^2.6.0",
"fastify-formbody": "^3.1.0",
......
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