Skip to content
Open
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
26 changes: 12 additions & 14 deletions app/api/core/application/CreateEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PropertyAssignmentCreatorServiceStrategy } from './propertyAssignmentCr

type Input = {
propertyAssignments: PropertyAssignmentInput[];
inputFiles: InputFile[];
inputFiles?: InputFile[];
templateId?: string;
icon?: EntityIcon;
};
Expand All @@ -22,33 +22,31 @@ type Deps = {
};

class CreateEntityUseCase extends AbstractUseCase<Input, Output, Deps> {
protected async executeAsync({
templateId,
icon,
propertyAssignments: propertyAssignmentsInput,
inputFiles,
}: Input): Promise<Output> {
protected async executeAsync(input: Input): Promise<Output> {
const entity = await this.deps.entitiesService.create({
templateId,
icon,
templateId: input.templateId,
icon: input.icon,
userId: this.actor?.id,
});

const propertyAssignments = await this.deps.propertyAssignmentCreatorServiceStrategy.bulkCreate(
propertyAssignmentsInput,
input.propertyAssignments,
entity.template,
inputFiles.filter(f => f.isAttachment())
input?.inputFiles?.filter(f => f.isAttachment()) || []
);

entity.setPropertyAssignmentsInAllLanguages(propertyAssignments, true);

const attachments = await this.deps.fileService.fromInputFiles(entity.sharedId, inputFiles);
const documentsOrAttachments = await this.deps.fileService.fromInputFiles(
entity.sharedId,
input?.inputFiles || []
);

await this.deps.fileService.storeFiles(attachments);
await this.deps.fileService.storeFiles(documentsOrAttachments);

await this.transactionManager.run(async () => {
await this.deps.entitiesService.insert(entity);
await this.deps.fileService.insert(attachments);
await this.deps.fileService.insert(documentsOrAttachments);
});

return entity;
Expand Down
1 change: 0 additions & 1 deletion app/api/core/application/specs/CreateEntity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ describe('CreateEntityUseCase', () => {
const entity = await sut.execute({
templateId: factory.id('Document').toHexString(),
propertyAssignments: [{ name: 'title', value: [{ value: 'My entity title' }] }],
inputFiles: [],
});

const entities = await testingEnvironment.db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('ImagePropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'url_image',
type: 'image',
value: [{ value: 'https://example.com/image.jpg' }],
Expand Down Expand Up @@ -96,6 +97,7 @@ describe('ImagePropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'attached_image_1',
type: 'image',
value: [{ value: '/api/files/def456.png' }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'url_media',
type: 'media',
value: [{ value: 'https://www.youtube.com/watch?v=kvX9Hbg7q88' }],
Expand Down Expand Up @@ -96,6 +97,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'url_media',
type: 'media',
value: [
Expand Down Expand Up @@ -134,14 +136,15 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'attached_media_1',
type: 'media',
value: [{ value: '/api/files/video.mp4' }],
},
]);
});

it('should map an attachment with the correct index', async () => {
it('should throw error when attachment index is missing', async () => {
const { sut } = createSut();
const templateDBO = await testingEnvironment.db
.getCollection('templates')!
Expand All @@ -165,6 +168,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'attached_media_2',
type: 'media',
value: [{ value: '/api/files/audio.mp3' }],
Expand Down Expand Up @@ -193,6 +197,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'attached_media_1',
type: 'media',
value: [{ value: '(/api/files/video.mp4, {"timelinks":{"00:00:00":"titleeeeee"}})' }],
Expand Down Expand Up @@ -221,6 +226,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'attached_media_1',
type: 'media',
value: [{ value: '/api/files/video.mp4' }],
Expand Down Expand Up @@ -387,6 +393,7 @@ describe('MediaPropertyAssignmentCreatorService', () => {

expect(result).toEqual([
{
isTranslatable: true,
name: 'url_media',
type: 'media',
value: [],
Expand Down
Loading
Loading