Skip to content

Commit

Permalink
fix: icon for aft (#44)
Browse files Browse the repository at this point in the history
* fix: icon for aft

* fix: update faker

---------

Co-authored-by: Yana Sidarava <[email protected]>
  • Loading branch information
yana617 and Yana Sidarava authored Aug 3, 2024
1 parent ae24206 commit e3ac68e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 85 deletions.
36 changes: 23 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
"devDependencies": {
"@babel/eslint-parser": "7.21.8",
"@babel/plugin-transform-runtime": "7.22.4",
"@faker-js/faker": "^8.4.1",
"babel-jest": "29.5.0",
"eslint": "7.31.0",
"eslint-config-airbnb": "18.2.1",
"eslint-plugin-import": "2.23.4",
"faker": "5.5.3",
"jest": "27.0.0",
"nock": "13.1.4",
"nodemon": "2.0.22",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/additionalFieldTemplate.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getAllAft = async (req, res) => {
};

const createAft = async (req, res) => {
const { label, description, icon = '' } = req.body;
const { label, description, icon } = req.body;
const newAft = await aftRepository.create({ label, description, icon });

const users = await userRepository.getAll();
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/validators/validateAdditionalFieldTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default checkSchema({
in: ['body'],
isLength: {
errorMessage: ERRORS.AFT_ICON_ERROR,
options: { min: 10, max: 150 },
options: { min: 1, max: 10 },
},
optional: { options: { nullable: true } },
exists: true,
},
});
117 changes: 68 additions & 49 deletions src/seeders/20210810133015-create-roles-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ const bcrypt = require('bcrypt');

const { NODE_ENV } = process.env;

const roles = [
'USER',
'VOLUNTEER',
'ADMIN',
];
const roles = ['USER', 'VOLUNTEER', 'ADMIN'];
const rolesTranslates = {
USER: 'Пользователь',
VOLUNTEER: 'Волонтер',
Expand All @@ -31,10 +27,7 @@ const permissions = [
'CREATE_CLAIM_FOR_UNREGISTERED_USERS',
];
const rolePermissions = {
USER: [
'VIEW_PROFILE',
'EDIT_PROFILE',
],
USER: ['VIEW_PROFILE', 'EDIT_PROFILE'],
VOLUNTEER: [
'VIEW_PROFILE',
'EDIT_PROFILE',
Expand All @@ -61,31 +54,34 @@ const rolesToInsert = roles.map((role) => {
const salt = bcrypt.genSaltSync(10);
const salt2 = bcrypt.genSaltSync(10);
const password = '111111';
const users = [{
id: v4(),
name: 'Admin',
surname: 'Administrator',
phone: '375291111111',
email: '[email protected]',
birthday: new Date('1980'),
hash: bcrypt.hashSync(password, salt),
salt,
role_id: rolesInDb.ADMIN,
createdAt: new Date(),
updatedAt: new Date(),
}, {
id: v4(),
name: 'User',
surname: 'Userski',
phone: '375291111112',
email: '[email protected]',
birthday: new Date('1999'),
hash: bcrypt.hashSync(password, salt2),
salt: salt2,
role_id: rolesInDb.USER,
createdAt: new Date(),
updatedAt: new Date(),
}];
const users = [
{
id: v4(),
name: 'Admin',
surname: 'Administrator',
phone: '375291111111',
email: '[email protected]',
birthday: new Date('1980'),
hash: bcrypt.hashSync(password, salt),
salt,
role_id: rolesInDb.ADMIN,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: v4(),
name: 'User',
surname: 'Userski',
phone: '375291111112',
email: '[email protected]',
birthday: new Date('1999'),
hash: bcrypt.hashSync(password, salt2),
salt: salt2,
role_id: rolesInDb.USER,
createdAt: new Date(),
updatedAt: new Date(),
},
];

const usersToInsert = NODE_ENV === 'development' ? users : [];

Expand All @@ -101,19 +97,42 @@ const permissionsToInsert = permissions.map((permission) => {
});

module.exports = {
up: async (queryInterface) => Promise.all([
queryInterface.bulkInsert('roles', rolesToInsert),
queryInterface.bulkInsert('permissions', permissionsToInsert),
...Object.keys(rolePermissions).map((role) => queryInterface
.bulkInsert('role_permissions', rolePermissions[role].map((permission) => ({
id: v4(),
role_id: rolesInDb[role],
permission_id: permissionsInDb[permission],
createdAt: new Date(),
updatedAt: new Date(),
})))),
...(NODE_ENV === 'development' ? [
queryInterface.bulkInsert('users', usersToInsert),
] : []),
]),
up: async (queryInterface) => {
// await queryInterface.bulkDelete('roles', null, {
// truncate: true,
// cascade: true,
// });
// await queryInterface.bulkDelete('permissions', null, {
// truncate: true,
// cascade: true,
// });
// await queryInterface.bulkDelete('users', null, {
// truncate: true,
// cascade: true,
// });
// await queryInterface.bulkDelete('role_permissions', null, {
// truncate: true,
// cascade: true,
// });

await queryInterface.bulkInsert('roles', rolesToInsert);
await queryInterface.bulkInsert('permissions', permissionsToInsert);

const queries = Object.keys(rolePermissions).map((role) =>
queryInterface.bulkInsert(
'role_permissions',
rolePermissions[role].map((permission) => ({
id: v4(),
role_id: rolesInDb[role],
permission_id: permissionsInDb[permission],
createdAt: new Date(),
updatedAt: new Date(),
}))
)
);
await Promise.all(queries);
if (NODE_ENV === 'development') {
await queryInterface.bulkInsert('users', usersToInsert);
}
},
};
36 changes: 21 additions & 15 deletions src/tests/additional-field-templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,24 @@ describe('POST /additional-field-templates', () => {
expect(aftInDb.label).toEqual(aftOne.label);

// expect uaf was created
const uafInDb = await db.UserAdditionalField.findAll({ where: { user_id: userOne.id } });
const uafInDb = await db.UserAdditionalField.findAll({
where: { user_id: userOne.id },
});
expect(uafInDb).toBeDefined();
expect(uafInDb.length).toBe(1);
});

test('Should save without icon and return new additional field template', async () => {
test('Should not save without icon and return new additional field template', async () => {
const aftOne = generateAft();
const token = await createUserAndGetToken(generateUser(), 'ADMIN');
const response = await request(app)
.post('/additional-field-templates')
.send({ ...aftOne, icon: undefined })
.set('x-access-token', token)
.expect(201);
.expect(400);

const { data: aft } = response.body;
expect(aft.label).toEqual(aftOne.label);
const aftInDb = await db.AdditionalFieldTemplate.findByPk(aft.id);
expect(aftInDb).toBeDefined();
expect(aftInDb.label).toEqual(aftOne.label);
const { errors } = response.body;
expect(errors).toBeDefined();
});

test('Should fail with validation errors', async () => {
Expand Down Expand Up @@ -153,26 +152,33 @@ describe('DELETE /additional-field-templates', () => {
const aftOne = generateAft();
await db.AdditionalFieldTemplate.create(aftOne);
const token = await createUserAndGetToken(generateUser(), 'ADMIN');
await db.UserAdditionalField
.create({ user_id: userOne.id, additional_field_template_id: aftOne.id, value: false });
await db.UserAdditionalField.create({
user_id: userOne.id,
additional_field_template_id: aftOne.id,
value: false,
});

const aftInDb = await db.AdditionalFieldTemplate.findByPk(aftOne.id);
expect(aftInDb).toBeDefined();

const uafInDb = await db.UserAdditionalField
.findOne({ where: { additional_field_template_id: aftOne.id } });
const uafInDb = await db.UserAdditionalField.findOne({
where: { additional_field_template_id: aftOne.id },
});
expect(uafInDb).toBeDefined();

await request(app)
.delete(`/additional-field-templates/${aftOne.id}`)
.set('x-access-token', token)
.expect(200);

const aftInDbAfterDelete = await db.AdditionalFieldTemplate.findByPk(aftOne.id);
const aftInDbAfterDelete = await db.AdditionalFieldTemplate.findByPk(
aftOne.id,
);
expect(aftInDbAfterDelete).toBeNull();

const uafInDbAfterDelete = await db.UserAdditionalField
.findOne({ where: { additional_field_template_id: aftOne.id } });
const uafInDbAfterDelete = await db.UserAdditionalField.findOne({
where: { additional_field_template_id: aftOne.id },
});
expect(uafInDbAfterDelete).toBeNull();
});
});
4 changes: 2 additions & 2 deletions src/tests/fixtures/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bcrypt from 'bcrypt';
import { v4 } from 'uuid';
import faker from 'faker';
import { faker } from '@faker-js/faker';

import db from '#database';
import { DEFAULT_ROLE } from '#database/constants';
Expand Down Expand Up @@ -28,7 +28,7 @@ const generateAft = () => ({
id: v4(),
label: faker.lorem.words(2),
description: faker.lorem.words(15),
icon: faker.image.imageUrl(),
icon: faker.string.sample(5),
});

const generateUaf = (user_id, additional_field_template_id) => ({
Expand Down
4 changes: 2 additions & 2 deletions src/translations/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
INVALID_BIRTHDAY: 'Invalid birthday',
AFT_LABEL_ERROR: 'Label should be from 2 to 30 characters',
AFT_DESCRIPTION_ERROR: 'Description should be from 50 to 150 characters',
AFT_ICON_ERROR: 'Icon should be from 10 to 150 characters',
AFT_ICON_ERROR: 'Icon should be from 1 to 10 characters',
HISTORY_ACTION_CREATING_ERROR: 'History action was not created',
},
RU: {
Expand Down Expand Up @@ -70,7 +70,7 @@ export default {
INVALID_BIRTHDAY: 'Некорректная дата рождения',
AFT_LABEL_ERROR: 'Название должно быть от 2 до 30 символов',
AFT_DESCRIPTION_ERROR: 'Описание должно быть от 50 до 150 символов',
AFT_ICON_ERROR: 'Иконка должна быть от 10 до 150 символов',
AFT_ICON_ERROR: 'Иконка должна быть от 1 до 10 символов',
HISTORY_ACTION_CREATING_ERROR: 'Не удалось создать запись для истории действий',
},
};

0 comments on commit e3ac68e

Please sign in to comment.