mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
feat: link and unlink document to resource model
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import mime from 'mime-types';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Response } from 'express';
|
||||
import { param } from 'express-validator';
|
||||
import { body, param } from 'express-validator';
|
||||
import BaseController from '@/api/controllers/BaseController';
|
||||
import { Request } from 'express-validator/src/base';
|
||||
import { AttachmentsApplication } from '@/services/Attachments/AttachmentsApplication';
|
||||
@@ -34,6 +34,23 @@ export class AttachmentsController extends BaseController {
|
||||
this.validationResult,
|
||||
this.getAttachment.bind(this)
|
||||
);
|
||||
router.post(
|
||||
'/:id/link',
|
||||
[body('modelRef').exists(), body('modelId').exists()],
|
||||
this.validationResult
|
||||
);
|
||||
router.post(
|
||||
'/:id/link',
|
||||
[body('modelRef').exists(), body('modelId').exists()],
|
||||
this.validationResult,
|
||||
this.linkDocument.bind(this)
|
||||
);
|
||||
router.post(
|
||||
'/:id/unlink',
|
||||
[body('modelRef').exists(), body('modelId').exists()],
|
||||
this.validationResult,
|
||||
this.unlinkDocument.bind(this)
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
@@ -50,11 +67,12 @@ export class AttachmentsController extends BaseController {
|
||||
const file = req.file;
|
||||
|
||||
try {
|
||||
await this.attachmentsApplication.upload(tenantId, file);
|
||||
const data = await this.attachmentsApplication.upload(tenantId, file);
|
||||
|
||||
return res.status(200).send({
|
||||
status: 200,
|
||||
message: 'The document has uploaded successfully.',
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -90,10 +108,10 @@ export class AttachmentsController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
* @param req
|
||||
* @param res
|
||||
* @param next
|
||||
* Deletes the given document key.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
*/
|
||||
private async deleteAttachment(req: Request, res: Response, next: Function) {
|
||||
@@ -111,4 +129,60 @@ export class AttachmentsController extends BaseController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Links the given document key.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
*/
|
||||
private async linkDocument(req: Request, res: Response, next: Function) {
|
||||
const { tenantId } = req;
|
||||
const { id: documentId } = req.params;
|
||||
const { modelRef, modelId } = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.attachmentsApplication.link(
|
||||
tenantId,
|
||||
documentId,
|
||||
modelRef,
|
||||
modelId
|
||||
);
|
||||
return res.status(200).send({
|
||||
status: 200,
|
||||
message: 'The document has been linked successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Links the given document key.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
*/
|
||||
private async unlinkDocument(req: Request, res: Response, next: Function) {
|
||||
const { tenantId } = req;
|
||||
const { id: documentId } = req.params;
|
||||
const { modelRef, modelId } = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.attachmentsApplication.link(
|
||||
tenantId,
|
||||
documentId,
|
||||
modelRef,
|
||||
modelId
|
||||
);
|
||||
return res.status(200).send({
|
||||
status: 200,
|
||||
message: 'The document has been linked successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ import { ImportController } from './controllers/Import/ImportController';
|
||||
import { BankingController } from './controllers/Banking/BankingController';
|
||||
import { Webhooks } from './controllers/Webhooks/Webhooks';
|
||||
import { ExportController } from './controllers/Export/ExportController';
|
||||
import { AttachmentsController } from './controllers/AttachmentsController';
|
||||
import { AttachmentsController } from './controllers/Attachments/AttachmentsController';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
@@ -71,7 +71,7 @@ export default () => {
|
||||
// ---------------------------
|
||||
app.use(asyncRenderMiddleware);
|
||||
app.use(I18nMiddleware);
|
||||
|
||||
|
||||
app.use('/auth', Container.get(Authentication).router());
|
||||
app.use('/invite', Container.get(InviteUsers).nonAuthRouter());
|
||||
app.use('/subscription', Container.get(SubscriptionController).router());
|
||||
@@ -80,7 +80,6 @@ export default () => {
|
||||
app.use('/jobs', Container.get(Jobs).router());
|
||||
app.use('/account', Container.get(Account).router());
|
||||
app.use('/webhooks', Container.get(Webhooks).router());
|
||||
app.use('/attachments', Container.get(AttachmentsController).router());
|
||||
|
||||
// - Dashboard routes.
|
||||
// ---------------------------
|
||||
@@ -145,6 +144,7 @@ export default () => {
|
||||
dashboard.use('/tax-rates', Container.get(TaxRatesController).router());
|
||||
dashboard.use('/import', Container.get(ImportController).router());
|
||||
dashboard.use('/export', Container.get(ExportController).router());
|
||||
dashboard.use('/attachments', Container.get(AttachmentsController).router());
|
||||
|
||||
dashboard.use('/', Container.get(ProjectTasksController).router());
|
||||
dashboard.use('/', Container.get(ProjectTimesController).router());
|
||||
|
||||
Reference in New Issue
Block a user