feat: link and unlink document to resource model

This commit is contained in:
Ahmed Bouhuolia
2024-05-24 19:50:06 +02:00
parent c8f31f33be
commit 8f904fae3a
14 changed files with 347 additions and 20 deletions

View File

@@ -0,0 +1,39 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import {
validateLinkModelEntryExists,
validateLinkModelExists,
} from './_utils';
@Service()
export class UnlinkAttachment {
@Inject()
private tenancy: HasTenancyService;
/**
*
* @param {number} tenantId
* @param {string} filekey
* @param {string} modelRef
* @param {number} modelId
*/
async unlink(
tenantId: number,
filekey: string,
modelRef: string,
modelId: number
) {
const { DocumentLink, ...models } = this.tenancy.models(tenantId);
const LinkModel = models[modelRef];
validateLinkModelExists(LinkModel);
const foundLinkModel = await LinkModel.query().findById(modelId);
validateLinkModelEntryExists(foundLinkModel);
// Delete the document link.
await DocumentLink.query()
.where('modelRef', modelRef)
.where('modelId', modelId)
.delete();
}
}