Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

515 helper link functionality #530

Merged
merged 19 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 65 additions & 28 deletions backend/migrations/0001-1.0-helperlink.js
erenfn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,78 @@ module.exports = {
up: async (queryInterface, Sequelize) => {
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.createTable(TABLE_NAME, {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
await queryInterface.createTable(
TABLE_NAME,
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
title: {
type: Sequelize.STRING(255),
allowNull: false,
},
headerBackgroundColor: {
type: Sequelize.STRING(15),
allowNull: false,
defaultValue: '#F8F9F8',
},
linkFontColor: {
type: Sequelize.STRING(15),
allowNull: false,
defaultValue: '#344054',
},
iconColor: {
type: Sequelize.STRING(15),
allowNull: false,
defaultValue: '#7F56D9',
},
createdBy: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id',
},
},
},
title: {
{ transaction }
);

await queryInterface.addColumn(
TABLE_NAME,
'url',
{
type: Sequelize.STRING(255),
allowNull: false,
defaultValue: '/',
},
headerBackgroundColor: {
type: Sequelize.STRING(15),
allowNull: false,
defaultValue : '#F8F9F8'
},
linkFontColor: {
type: Sequelize.STRING(15),
{ transaction }
);

await queryInterface.addColumn(
TABLE_NAME,
'active',
{
type: Sequelize.BOOLEAN,
defaultValue: true,
allowNull: false,
defaultValue : '#344054'
},
iconColor: {
type: Sequelize.STRING(15),
{ transaction }
);

await queryInterface.addColumn(
TABLE_NAME,
'absolutePath',
{
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false,
defaultValue: '#7F56D9'
},
createdBy: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
}
}, { transaction });
{ transaction }
);

// Commit the transaction
await transaction.commit();
Expand All @@ -64,5 +101,5 @@ module.exports = {
await transaction.rollback();
throw error;
}
}
},
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
};
47 changes: 34 additions & 13 deletions backend/src/models/HelperLink.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { validateHexColor } = require("../utils/guide.helper");
const { validateHexColor } = require('../utils/guide.helper');
const { validateUrl } = require('../utils/link.helper');

/**
*
Expand All @@ -8,7 +9,7 @@ const { validateHexColor } = require("../utils/guide.helper");
*/
module.exports = (sequelize, DataTypes) => {
const HelperLink = sequelize.define(
"HelperLink",
'HelperLink',
{
id: {
type: DataTypes.INTEGER,
Expand All @@ -25,52 +26,72 @@ module.exports = (sequelize, DataTypes) => {
headerBackgroundColor: {
type: DataTypes.STRING(15),
allowNull: false,
defaultValue: "#F8F9F8",
defaultValue: '#F8F9F8',
validate: {
isHexColor(value) {
validateHexColor(value, "headerBackgroundColor");
validateHexColor(value, 'headerBackgroundColor');
},
},
},
linkFontColor: {
type: DataTypes.STRING(15),
allowNull: false,
defaultValue: "#344054",
defaultValue: '#344054',
validate: {
isHexColor(value) {
validateHexColor(value, "linkFontColor");
validateHexColor(value, 'linkFontColor');
},
},
},
iconColor: {
type: DataTypes.STRING(15),
allowNull: false,
defaultValue: "#7F56D9",
defaultValue: '#7F56D9',
validate: {
isHexColor(value) {
validateHexColor(value, "iconColor");
validateHexColor(value, 'iconColor');
},
},
},
createdBy: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
model: 'users',
key: 'id',
},
},
url: {
type: DataTypes.STRING(255),
allowNull: false,
validate: {
customValidation(value) {
return validateUrl(value);
},
},
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
defaultValue: '/',
},
active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
},
absolutePath: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
tableName: "helper_link",
tableName: 'helper_link',
timestamps: false,
}
);

HelperLink.associate = (models) => {
HelperLink.belongsTo(models.User, {
foreignKey: "createdBy",
as: "creator",
foreignKey: 'createdBy',
as: 'creator',
});
};

Expand Down
21 changes: 10 additions & 11 deletions backend/src/test/e2e/helperLink.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ describe('E2e tests helperLink', () => {
if (helper.createdBy === 2) {
expect(res.body).to.not.include(helper);
} else {
const {
createdBy: c,
id: i,
...curr
} = res.body.find((it) => it.title === helper.title);
const { createdBy: c, id: i, ...curr } = res.body.find((it) => it.title === helper.title);
const { createdBy, links, id, ...expected } = helper;
expect(curr).to.be.deep.equal(expected);
// expect(creator).to.have.property("id", createdBy);
Expand Down Expand Up @@ -272,11 +268,7 @@ describe('E2e tests helperLink', () => {
.set('Authorization', `Bearer ${token}`);
expect(res).to.have.status(200);
mocks.HelperLinkList.forEach((helper) => {
const {
createdBy: c,
id: i,
...curr
} = res.body.find((it) => it.title === helper.title);
const { createdBy: c, id: i, ...curr } = res.body.find((it) => it.title === helper.title);
const { createdBy, links, id, ...expected } = helper;
expect(curr).to.be.deep.equal(expected);
// expect(creator).to.have.property("id", createdBy);
Expand Down Expand Up @@ -375,7 +367,14 @@ describe('E2e tests helperLink', () => {
.set('Authorization', `Bearer ${token}`);
expect(res).to.have.status(400);
expect(res.body).to.be.deep.equal({
errors: ['ID must be an integer', 'Header is required', 'links must be an array'],
errors: [
'ID must be an integer',
'Header is required',
'Invalid value for url',
'Invalid value for url',
'Invalid value for absolutePath',
'links must be an array',
],
});
});
it('should return 400 if title is missing', async () => {
Expand Down
3 changes: 3 additions & 0 deletions backend/src/test/e2e/link.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ describe('E2e tests link', () => {
linkFontColor: '#344054',
iconColor: '#7F56D9',
createdBy: 1,
absolutePath: true,
active: true,
url: '/url',
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
},
});
});
Expand Down
49 changes: 24 additions & 25 deletions backend/src/test/mocks/helperLink.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class LinkBuilder {
this.link = {
id: id ?? 1,
title: `Link ${id ?? 1}`,
url: "/url",
url: '/url',
order: 1,
target: true,
helperId,
Expand All @@ -14,7 +14,7 @@ class LinkBuilder {
}

missingTitle() {
this.link.title = "";
this.link.title = '';
return this;
}

Expand All @@ -24,17 +24,17 @@ class LinkBuilder {
}

missingUrl() {
this.link.url = "";
this.link.url = '';
return this;
}

invalidUrl() {
this.link.url = "url";
this.link.url = 'url';
return this;
}

invalidOrderType() {
this.link.order = "order";
this.link.order = 'order';
return this;
}

Expand Down Expand Up @@ -68,13 +68,14 @@ class HelperLinkBuilder {
this.helperLink = {
id: id ?? 1,
title: `Helper Link ${id ?? 1}`,
headerBackgroundColor: "#F8F9F8",
linkFontColor: "#344054",
iconColor: "#7F56D9",
headerBackgroundColor: '#F8F9F8',
linkFontColor: '#344054',
iconColor: '#7F56D9',
createdBy: 1,
links: Array.from({ length: 5 }, (_, i) =>
LinkBuilder.link(i + 1, id).build()
),
links: Array.from({ length: 5 }, (_, i) => LinkBuilder.link(i + 1, id).build()),
url: '/url',
absolutePath: true,
active: true,
};
}

Expand All @@ -83,22 +84,22 @@ class HelperLinkBuilder {
}

missingTitle() {
this.helperLink.title = "";
this.helperLink.title = '';
return this;
}

invalidHeaderBackgroundColor() {
this.helperLink.headerBackgroundColor = "color";
this.helperLink.headerBackgroundColor = 'color';
return this;
}

invalidLinkFontColor() {
this.helperLink.linkFontColor = "color";
this.helperLink.linkFontColor = 'color';
return this;
}

invalidIconColor() {
this.helperLink.iconColor = "color";
this.helperLink.iconColor = 'color';
return this;
}

Expand All @@ -112,17 +113,15 @@ class HelperLinkBuilder {
}
}

const LinksList = Array.from({ length: 5 }, (_, i) =>
LinkBuilder.link(i + 1).build()
);
const LinksList = Array.from({ length: 5 }, (_, i) => LinkBuilder.link(i + 1).build());

const HelperLinkList = Array.from({ length: 10 }, (_, i) =>
HelperLinkBuilder.helperLink(i + 1).build()
).map((link, i) => {
if (i < 5) link.createdBy = 1;
else link.createdBy = 2;
return link;
});
const HelperLinkList = Array.from({ length: 10 }, (_, i) => HelperLinkBuilder.helperLink(i + 1).build()).map(
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
(link, i) => {
if (i < 5) link.createdBy = 1;
else link.createdBy = 2;
return link;
}
);

module.exports = {
HelperLinkBuilder,
Expand Down
3 changes: 3 additions & 0 deletions backend/src/utils/helperLink.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const helperValidator = [
.withMessage('Invalid value for headerBackgroundColor'),
body('linkFontColor').optional().custom(isValidHexColor).withMessage('Invalid value for linkFontColor'),
body('iconColor').optional().custom(isValidHexColor).withMessage('Invalid value for iconColor'),
body('url').isString().withMessage('Invalid value for url').custom(validateUrl).withMessage('Invalid value for url'),
body('active').optional().isBoolean().withMessage('Invalid value for active'),
body('absolutePath').isBoolean().withMessage('Invalid value for absolutePath'),
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
body('links').isArray().withMessage('links must be an array'),
body('links.*.title')
.trim()
Expand Down
Loading