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

Add Custom Fields Feature for Project Issues and Implement Associated CRUD Operations #21

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions backend/src/db/migrations/20231228152900-add-custom-fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const TABLE_NAME = 'project_custom_fields';

/** @type {import('sequelize-cli').Migration} */
export default {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(TABLE_NAME, {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
projectId: {
type: Sequelize.INTEGER,
field: 'project_id',
references: {
model: {
tableName: 'projects',
schema: 'public',
},
key: 'id',
},
},
fieldName: {
type: Sequelize.STRING,
field: 'field_name',
},
fieldType: {
type: Sequelize.STRING,
field: 'field_type',
},
createdAt: {
field: 'created_at',
type: Sequelize.DATE,
},
updatedAt: {
field: 'updated_at',
type: Sequelize.DATE,
},
});

await queryInterface.addIndex(TABLE_NAME, {
fields: ['project_id'],
unique: false,
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable(TABLE_NAME);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const TABLE_NAME = 'issues';
const COLUMN_NAME = 'custom_fields';

/** @type {import('sequelize-cli').Migration} */
export default {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, {
type: Sequelize.JSONB,
});
},
async down(queryInterface, Sequelize) {
await queryInterface.removeColumn(TABLE_NAME, COLUMN_NAME);
},
};
20 changes: 11 additions & 9 deletions backend/src/db/models/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict';

import { DataTypes, Sequelize } from 'sequelize';
import { ENABLE_SEQUELIZE_LOGGING, SQL_URI } from '../../services/config.js';

import User from './user.js';
import Project from './project.js';
import Board from './board.js';
import IssueStatuses from './issue-statuses.js';
import Issue from './issue.js';
import { ENABLE_SEQUELIZE_LOGGING, SQL_URI } from '../../services/config.js';
import Asset from './asset.js';
import IssueComment from './issue-comment.js';
import ProjectTag from './project-tag.js';
import IssueTag from './issue-tag.js';
import Board from './board.js';
import IssueBoard from './issue-board.js';
import IssueComment from './issue-comment.js';
import IssueLinks from './issue-links.js';
import IssueStatuses from './issue-statuses.js';
import IssueTag from './issue-tag.js';
import Issue from './issue.js';
import ProjectCustomFields from './project-custom-fields.js';
import ProjectPermissions from './project-permissions.js';
import ProjectTag from './project-tag.js';
import Project from './project.js';
import User from './user.js';

export const db = {};

Expand All @@ -35,6 +36,7 @@ const init = async () => {
db.IssueBoard = IssueBoard(db.sequelize, DataTypes);
db.IssueLinks = IssueLinks(db.sequelize, DataTypes);
db.ProjectPermissions = ProjectPermissions(db.sequelize, DataTypes);
db.ProjectCustomFields = ProjectCustomFields(db.sequelize, DataTypes);

Object.values(db).forEach((model) => {
if (model.associate) {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/db/models/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export default (sequelize, DataTypes) => {
type: DataTypes.TSVECTOR,
field: 'vector_search',
},
customFields: {
type: DataTypes.JSONB,
field: 'custom_fields',
},
createdAt: {
field: 'created_at',
type: DataTypes.DATE,
Expand Down
51 changes: 51 additions & 0 deletions backend/src/db/models/project-custom-fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

export default (sequelize, DataTypes) => {
const ProjectCustomField = sequelize.define(
'ProjectCustomField',
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
projectId: {
type: DataTypes.INTEGER,
field: 'project_id',
references: {
model: 'projects',
key: 'id',
},
},
fieldName: {
type: DataTypes.STRING,
field: 'field_name',
},
fieldType: {
type: DataTypes.STRING,
field: 'field_type',
},
createdAt: {
field: 'created_at',
type: DataTypes.DATE,
},
updatedAt: {
field: 'updated_at',
type: DataTypes.DATE,
},
},
{
sequelize,
tableName: 'project_custom_fields',
timestamps: false,
indexes: [{ unique: false, fields: ['project_id'] }],
}
);

ProjectCustomField.associate = ({ Project }) => {
ProjectCustomField.belongsTo(Project, { foreignKey: 'project_id' });
};

return ProjectCustomField;
};
2 changes: 2 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ const myContextFunction = async (request) => {
const token = request.headers.authorization;
let user = null;

if (!token) return { db, user };

// Allow if introspection query only
if (!Array.isArray(request.body) && request?.body?.query?.includes('IntrospectionQuery')) {
return {
Expand Down
43 changes: 42 additions & 1 deletion backend/src/resolvers/Issue/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { keyBy, merge, values } from 'lodash-es';
import { Op } from 'sequelize';
import yn from 'yn';

import { websocketBroadcast } from '../../services/ws-server.js';

Expand Down Expand Up @@ -119,7 +121,19 @@ const resolvers = {
return { message: 'success', status: 'success' };
},
updateIssue: async (parent, { input }, { db, websocketServer }) => {
const { id, issueStatusId, assigneeId, reporterId, title, description, tagIds, priority, archived } = input;
const {
id,
issueStatusId,
assigneeId,
reporterId,
title,
description,
tagIds,
priority,
archived,
customFieldId,
customFieldValue,
} = input;

const issue = await db.sequelize.models.Issue.findByPk(id);

Expand All @@ -144,6 +158,28 @@ const resolvers = {
if (issue.assigneeId === 0) issue.assigneeId = null;
if (issue.reporterId === 0) issue.reporterId = null;

if (customFieldId && customFieldValue) {
const customField = await db.sequelize.models.ProjectCustomField.findByPk(Number(customFieldId));
if (!customField) throw new Error('Custom field not found');

let valueCasted = customFieldValue;

if (customField.fieldType.toLowerCase() === 'number') valueCasted = Number(customFieldValue);
else if (customField.fieldType.toLowerCase() === 'boolean') valueCasted = yn(customFieldValue);

const customFieldObject = {
id: `${issue.id}-${customField.id}`,
customFieldId,
value: valueCasted,
createdAt: new Date(), // TODO: improve date format decision
};

// TODO: investigate how to deep set the value instead of this to leverage DB level updating
issue.customFields = issue.customFields
? values(merge(keyBy(issue.customFields, 'id'), keyBy([customFieldObject], 'id')))
: [customFieldObject];
}

await issue.save();

const issueStatus = await db.sequelize.models.IssueStatuses.findByPk(issueStatusId ?? issue.issueStatusId);
Expand Down Expand Up @@ -202,6 +238,11 @@ const resolvers = {
return { message: 'issue deleted', status: 'success' };
},
},
CustomFieldValue: {
customField: async (parent, args, { db }) => {
return db.sequelize.models.ProjectCustomField.findByPk(parent.customFieldId);
},
},
Issue: {
links: async (parent, args, { db }) => {
return [
Expand Down
28 changes: 25 additions & 3 deletions backend/src/resolvers/Project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ const resolvers = {
},
},
Mutation: {
createProjectCustomField: async (parent, { input: { projectId, fieldName, fieldType } }, { db }) =>
await db.sequelize.models.ProjectCustomField.create(
{
projectId: Number(projectId),
fieldName,
fieldType,
},
{ returning: true }
),
deleteProjectCustomField: async (parent, { input: { id } }, { db }) => {
const findCustomField = await db.sequelize.models.ProjectCustomField.findByPk(id);

if (!findCustomField) throw new Error('Custom field not found');

await findCustomField.destroy();

return {
message: 'deleted custom field',
status: 'success',
};
},
createProjectTag: async (parent, { input }, { db }) => {
const { projectId, name } = input;

Expand All @@ -71,9 +92,7 @@ const resolvers = {
name,
});
},
deleteProjectTag: async (parent, { input }, { db }) => {
const { id } = input;

deleteProjectTag: async (parent, { input: { id } }, { db }) => {
const findProjectTag = await db.sequelize.models.ProjectTag.findByPk(id);

if (!findProjectTag) {
Expand Down Expand Up @@ -162,6 +181,9 @@ const resolvers = {
},
},
Project: {
customFields: (parent, args, { db }) => {
return db.sequelize.models.ProjectCustomField.findAll({ where: { projectId: parent.id } });
},
tags: (parent, args, { db }) => {
return db.sequelize.models.ProjectTag.findAll({ where: { projectId: parent.id } });
},
Expand Down
46 changes: 46 additions & 0 deletions backend/src/type-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const typeDefs = gql`
PRIVATE
}

enum CUSTOM_FIELD_TYPE {
TEXT
NUMBER
DATE
BOOLEAN
}

enum Order {
ASC
DESC
Expand Down Expand Up @@ -77,6 +84,25 @@ const typeDefs = gql`
tags: [ProjectTag]
users: [User]
issueCount: Int
customFields: [CustomField]
}

type CustomField {
id: ID!
projectId: String!
fieldName: String!
fieldType: CUSTOM_FIELD_TYPE!
createdAt: String
updatedAt: String
}

type CustomFieldValue {
id: ID!
customFieldId: String!
customField: CustomField
value: String!
createdAt: String
updatedAt: String
}

type Issue {
Expand All @@ -98,6 +124,8 @@ const typeDefs = gql`
links: [Issue]
linkType: String
linkedIssueId: String

customFields: [CustomFieldValue]
}

type Column {
Expand Down Expand Up @@ -215,6 +243,11 @@ const typeDefs = gql`
priority: Int
archived: Boolean
tagIds: [String]

# This represents the ID of the project custom field id
customFieldId: String
# We will transform this value based on the custom field type
customFieldValue: String
}

input DeleteIssueInput {
Expand Down Expand Up @@ -328,6 +361,16 @@ const typeDefs = gql`
projectId: String!
}

input CreateProjectCustomFieldInput {
projectId: String!
fieldName: String!
fieldType: CUSTOM_FIELD_TYPE!
}

input DeleteProjectCustomFieldInput {
id: String!
}

# Mutations
type Mutation {
createProject(input: CreateProjectInput): Project
Expand All @@ -338,6 +381,9 @@ const typeDefs = gql`
createProjectTag(input: CreateProjectTagInput!): ProjectTag
deleteProjectTag(input: DeleteProjectTagInput!): MessageAndStatus

createProjectCustomField(input: CreateProjectCustomFieldInput!): CustomField
deleteProjectCustomField(input: DeleteProjectCustomFieldInput!): MessageAndStatus

createBoard(input: CreateBoardInput): Board
updateBoard(input: UpdateBoardInput): Board

Expand Down