Files
planka/server/api/controllers/boards/create.js

297 lines
8.8 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
2025-09-08 16:20:27 +02:00
/**
* @swagger
* /projects/{projectId}/boards:
2025-09-08 16:20:27 +02:00
* post:
* summary: Create board
* description: Creates a board within a project. Supports importing from Trello. Requires project manager permissions.
* tags:
* - Boards
* parameters:
* - name: projectId
* in: path
* required: true
* description: ID of the project to create the board in
* schema:
* type: string
* example: 1357158568008091264
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - position
* - name
* properties:
* position:
* type: number
* minimum: 0
* description: Position of the board within the project
* example: 65536
* name:
* type: string
* maxLength: 128
* description: Name/title of the board
* example: Development Board
* defaultView:
* type: string
* enum: [kanban, grid, list]
* default: kanban
* description: Default view for the board
* example: kanban
* defaultCardType:
* type: string
* enum: [project, story]
* default: project
* description: Default card type for new cards
* example: project
* limitCardTypesToDefaultOne:
* type: boolean
* default: false
* description: Whether to limit card types to default one
* example: false
* alwaysDisplayCardCreator:
* type: boolean
* default: false
* description: Whether to always display the card creator
* example: false
* expandTaskListsByDefault:
* type: boolean
* default: false
* description: Whether to expand task lists by default
* example: false
* multipart/form-data:
* schema:
* type: object
* required:
* - position
* - name
* properties:
* position:
* type: number
* minimum: 0
* description: Position of the board within the project
* example: 65536
* name:
* type: string
* maxLength: 128
* description: Name/title of the board
* example: Development Board
* defaultView:
* type: string
* enum: [kanban, grid, list]
* default: kanban
* description: Default view for the board
* example: kanban
* defaultCardType:
* type: string
* enum: [project, story]
* default: project
* description: Default card type for new cards
* example: project
* limitCardTypesToDefaultOne:
* type: boolean
* default: false
* description: Whether to limit card types to default one
* example: false
* alwaysDisplayCardCreator:
* type: boolean
* default: false
* description: Whether to always display the card creator
* example: false
* expandTaskListsByDefault:
* type: boolean
* default: false
* description: Whether to expand task lists by default
* example: false
* importType:
* type: string
* enum: [trello]
* description: Type of import
* example: trello
* importFile:
* type: string
* format: binary
* description: Import file
* requestId:
* type: string
* maxLength: 128
* description: Request ID for tracking
* example: req_123456
* responses:
* 200:
* description: Board created successfully
* content:
* application/json:
* schema:
* type: object
* required:
* - item
* - included
* properties:
* item:
* $ref: '#/components/schemas/Board'
* included:
* type: object
* required:
* - boardMemberships
* properties:
* boardMemberships:
* type: array
* items:
* $ref: '#/components/schemas/BoardMembership'
* 400:
* $ref: '#/components/responses/ValidationError'
* 401:
* $ref: '#/components/responses/Unauthorized'
* 404:
* $ref: '#/components/responses/NotFound'
* 422:
* description: Import file upload error
* content:
* application/json:
* schema:
* type: object
* required:
* - code
* - message
* properties:
* code:
* type: string
* description: Error code
* example: E_UNPROCESSABLE_ENTITY
* message:
* type: string
* enum:
* - No import file was uploaded
* - Invalid import file
* description: Specific error message
* example: No import file was uploaded
*/
const { idInput } = require('../../../utils/inputs');
2019-08-31 04:07:25 +05:00
const Errors = {
PROJECT_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
projectNotFound: 'Project not found',
},
NO_IMPORT_FILE_WAS_UPLOADED: {
noImportFileWasUploaded: 'No import file was uploaded',
},
INVALID_IMPORT_FILE: {
invalidImportFile: 'Invalid import file',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
projectId: {
...idInput,
required: true,
2019-08-31 04:07:25 +05:00
},
position: {
type: 'number',
min: 0,
required: true,
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
maxLength: 128,
required: true,
},
importType: {
type: 'string',
isIn: Object.values(Board.ImportTypes),
},
requestId: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
},
2019-08-31 04:07:25 +05:00
},
exits: {
2020-04-03 00:35:25 +05:00
projectNotFound: {
responseType: 'notFound',
},
noImportFileWasUploaded: {
responseType: 'unprocessableEntity',
},
invalidImportFile: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
2019-08-31 04:07:25 +05:00
},
2025-08-23 00:03:20 +02:00
async fn(inputs, exits) {
const { currentUser } = this.req;
const project = await Project.qm.getOneById(inputs.projectId);
2019-08-31 04:07:25 +05:00
if (!project) {
throw Errors.PROJECT_NOT_FOUND;
}
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
if (!isProjectManager) {
throw Errors.PROJECT_NOT_FOUND; // Forbidden
}
let boardImport;
if (inputs.importType) {
let files;
try {
2025-08-23 00:03:20 +02:00
files = await sails.helpers.utils.receiveFile(this.req.file('importFile'), false);
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
}
if (files.length === 0) {
throw Errors.NO_IMPORT_FILE_WAS_UPLOADED;
}
const file = _.last(files);
if (inputs.importType === Board.ImportTypes.TRELLO) {
const trelloBoard = await sails.helpers.boards
.processUploadedTrelloImportFile(file)
.intercept('invalidFile', () => Errors.INVALID_IMPORT_FILE);
boardImport = {
type: inputs.importType,
board: trelloBoard,
};
}
}
const values = _.pick(inputs, ['position', 'name']);
2022-12-26 21:10:50 +01:00
const { board, boardMembership } = await sails.helpers.boards.createOne.with({
values: {
...values,
project,
},
import: boardImport,
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
requestId: inputs.requestId,
request: this.req,
});
2019-08-31 04:07:25 +05:00
2025-08-23 00:03:20 +02:00
return exits.success({
2019-08-31 04:07:25 +05:00
item: board,
included: {
boardMemberships: [boardMembership],
},
2025-08-23 00:03:20 +02:00
});
},
2019-08-31 04:07:25 +05:00
};