WIP pass the failed tests.

This commit is contained in:
Ahmed Bouhuolia
2020-05-17 07:08:12 +02:00
parent 00de156c9f
commit 10f636d2bc
77 changed files with 2164 additions and 1403 deletions

View File

@@ -68,6 +68,10 @@ export default {
this.transferToAnotherAccount.validation,
asyncMiddleware(this.transferToAnotherAccount.handler));
router.post('/bulk/:type(activate|inactivate)',
this.bulkInactivateAccounts.validation,
asyncMiddleware(this.bulkInactivateAccounts.handler));
return router;
},
@@ -541,4 +545,47 @@ export default {
return res.status(200).send();
},
},
/**
* Bulk acvtivate/inactivate the given accounts.
*/
bulkInactivateAccounts: {
validation: [
query('ids').isArray({ min: 2 }),
query('ids.*').isNumeric().toInt(),
param('type').exists().isIn(['activate', 'inactivate']),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const filter = {
ids: [],
...req.query,
};
const { Account } = req.models;
const { type } = req.params;
const storedAccounts = await Account.query().whereIn('id', filter.ids);
const storedAccountsIds = storedAccounts.map((account) => account.id);
const notFoundAccounts = difference(filter.ids, storedAccountsIds);
if (notFoundAccounts.length > 0) {
return res.status(400).send({
errors: [{ type: 'ACCOUNTS.NOT.FOUND', code: 200 }],
});
}
const updatedAccounts = await Account.query()
.whereIn('id', storedAccountsIds)
.patch({
active: type === 'activate' ? 1 : 0,
});
return res.status(200).send({ ids: storedAccountsIds });
}
}
};

View File

@@ -7,6 +7,7 @@ import Mustache from 'mustache';
import jwt from 'jsonwebtoken';
import { pick } from 'lodash';
import uniqid from 'uniqid';
import moment from 'moment';
import Logger from '@/services/Logger';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
import SystemUser from '@/system/models/SystemUser';
@@ -19,6 +20,7 @@ import TenantsManager from '@/system/TenantsManager';
import TenantModel from '@/models/TenantModel';
import PasswordReset from '@/system/models/PasswordReset';
export default {
/**
* Constructor method.
@@ -51,7 +53,7 @@ export default {
login: {
validation: [
check('crediential').exists().isEmail(),
check('password').exists().isLength({ min: 4 }),
check('password').exists().isLength({ min: 5 }),
],
async handler(req, res) {
const validationErrors = validationResult(req);
@@ -87,7 +89,17 @@ export default {
errors: [{ type: 'USER_INACTIVE', code: 110 }],
});
}
// user.update({ last_login_at: new Date() });
const lastLoginAt = moment().format('YYYY/MM/DD HH:mm:ss');
const updateTenantUser = TenantUser.tenant().query()
.where('id', user.id)
.update({ last_login_at: lastLoginAt });
const updateSystemUser = SystemUser.query()
.where('id', user.id)
.update({ last_login_at: lastLoginAt });
await Promise.all([updateTenantUser, updateSystemUser]);
const token = jwt.sign(
{ email: user.email, _id: user.id },

View File

@@ -6,6 +6,7 @@ import {
validationResult,
} from 'express-validator';
import moment from 'moment';
import { difference } from 'lodash';
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
export default {
@@ -27,6 +28,10 @@ export default {
this.editExchangeRate.validation,
asyncMiddleware(this.editExchangeRate.handler));
router.delete('/bulk',
this.bulkDeleteExchangeRates.validation,
asyncMiddleware(this.bulkDeleteExchangeRates.handler));
router.delete('/:id',
this.deleteExchangeRate.validation,
asyncMiddleware(this.deleteExchangeRate.handler));
@@ -166,4 +171,39 @@ export default {
return res.status(200).send({ id });
},
},
bulkDeleteExchangeRates: {
validation: [
query('ids').isArray({ min: 2 }),
query('ids.*').isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const filter = {
ids: [],
...req.query,
};
const { ExchangeRate } = req.models;
const exchangeRates = await ExchangeRate.query().whereIn('id', filter.ids);
const exchangeRatesIds = exchangeRates.map((category) => category.id);
const notFoundExRates = difference(filter.ids, exchangeRatesIds);
if (notFoundExRates.length > 0) {
return res.status(400).send({
errors: [{ type: 'EXCHANGE.RATES.IS.NOT.FOUND', code: 200, ids: notFoundExRates }],
});
}
await ExchangeRate.query().whereIn('id', exchangeRatesIds).delete();
return res.status(200).send({ ids: exchangeRatesIds });
},
},
}

View File

@@ -5,6 +5,7 @@ import {
validationResult,
query,
} from 'express-validator';
import { difference } from 'lodash';
import asyncMiddleware from '../middleware/asyncMiddleware';
import {
DynamicFilter,
@@ -15,6 +16,7 @@ import {
mapFilterRolesToDynamicFilter,
} from '@/lib/ViewRolesBuilder';
export default {
/**
* Router constructor method.
@@ -31,6 +33,10 @@ export default {
this.newCategory.validation,
asyncMiddleware(this.newCategory.handler));
router.delete('/bulk',
this.bulkDeleteCategories.validation,
asyncMiddleware(this.bulkDeleteCategories.handler));
router.delete('/:id',
this.deleteItem.validation,
asyncMiddleware(this.deleteItem.handler));
@@ -43,6 +49,8 @@ export default {
this.getList.validation,
asyncMiddleware(this.getList.handler));
return router;
},
@@ -145,7 +153,7 @@ export default {
.where('id', id)
.update({ ...form });
return res.status(200).send({ id: updateItemCategory });
return res.status(200).send({ id });
},
},
@@ -274,8 +282,46 @@ export default {
errors: [{ type: 'CATEGORY_NOT_FOUND', code: 100 }],
});
}
return res.status(200).send({ category: item.toJSON() });
},
},
/**
* Bulk delete the given item categories.
*/
bulkDeleteCategories: {
validation: [
query('ids').isArray({ min: 2 }),
query('ids.*').isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const filter = {
ids: [],
...req.query,
};
const { ItemCategory } = req.models;
const itemCategories = await ItemCategory.query().whereIn('id', filter.ids);
const itemCategoriesIds = itemCategories.map((category) => category.id);
const notFoundCategories = difference(filter.ids, itemCategoriesIds);
if (notFoundCategories.length > 0) {
return res.status(400).send({
errors: [{ type: 'ITEM.CATEGORIES.IDS.NOT.FOUND', code: 200 }],
});
}
await ItemCategory.query().whereIn('id', filter.ids).delete();
return res.status(200).send({ ids: filter.ids });
},
},
};

View File

@@ -17,6 +17,7 @@ import Logger from '@/services/Logger';
const fsPromises = fs.promises;
export default {
/**
* Router constructor.
@@ -80,6 +81,7 @@ export default {
code: 'validation_error', ...validationErrors,
});
}
const { user } = req;
const form = {
custom_fields: [],
media_ids: [],
@@ -129,8 +131,10 @@ export default {
itemCategory,
inventoryAccount,
] = await Promise.all([
costAccountPromise, sellAccountPromise,
itemCategoryPromise, inventoryAccountPromise,
costAccountPromise,
sellAccountPromise,
itemCategoryPromise,
inventoryAccountPromise,
]);
if (!costAccount) {
errorReasons.push({ type: 'COST_ACCOUNT_NOT_FOUND', code: 100 });
@@ -152,11 +156,14 @@ export default {
const item = await Item.query().insertAndFetch({
name: form.name,
type: form.type,
sku: form.sku,
cost_price: form.cost_price,
sell_price: form.sell_price,
sell_account_id: form.sell_account_id,
cost_account_id: form.cost_account_id,
currency_code: form.currency_code,
category_id: form.category_id,
user_id: user.id,
note: form.note,
});
@@ -239,7 +246,7 @@ export default {
errorReasons.push({ type: 'ITEM_CATEGORY_NOT_FOUND', code: 140 });
}
const { attachment } = req.files;
const attachment = req.files && req.files.attachment ? req.files.attachment : null;
const attachmentsMimes = ['image/png', 'image/jpeg'];
// Validate the attachment.
@@ -277,7 +284,6 @@ export default {
cost_account_id: form.cost_account_id,
category_id: form.category_id,
note: form.note,
attachment_file: (attachment) ? item.attachmentFile : null,
});
// Save links of new inserted media that associated to the item model.