Skip to content

Commit

Permalink
chore: separate doc & entries logic more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Apr 8, 2024
1 parent a806043 commit 73062d2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
40 changes: 40 additions & 0 deletions packages/core/core/src/services/document-service/entries.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { UID } from '@strapi/types';
import { async } from '@strapi/utils';
import { assoc, omit } from 'lodash/fp';

import * as components from './components';

import { transformParamsDocumentId } from './transform/id-transform';
import { transformParamsToQuery } from './transform/query';
import { pickSelectionParams } from './params';
import { applyTransforms } from './attributes';
import { transformData } from './transform/data';
import entityValidator from '../entity-validator';

const createEntriesService = (uid: UID.ContentType) => {
Expand Down Expand Up @@ -72,10 +75,47 @@ const createEntriesService = (uid: UID.ContentType) => {
.update({ ...query, where: { id: entryToUpdate.id }, data: entryData });
}

async function publishEntry(entry: any, params = {} as any) {
async.pipe(
omit('id'),
assoc('publishedAt', new Date()),
// Updated at value is used to know if draft has been modified
// If both versions share the same value, it means the draft has not been modified
(draft) => assoc('updatedAt', draft.updatedAt, draft), // TODO: delete ?
assoc('documentId', entry.documentId), // TODO: delete ?
// Transform relations to target published versions
(draft) => {
// TODO: delete ? -> should be managed in createEntry already ? issue with allowMissingId
const opts = { uid, locale: draft.locale, status: 'published', allowMissingId: true };
return transformData(draft, opts);
},
// Create the published entry
(draft) => createEntry({ ...params, data: draft, locale: draft.locale, status: 'published' })
)(entry);
}

async function discardDraftEntry(entry: any, params = {} as any) {
async.pipe(
omit('id'),
assoc('publishedAt', null),
assoc('documentId', entry.documentId), // TODO: delete ?
// Transform relations to target draft versions
(entry) => {
// TODO: delete ? -> should be managed in createEntry already ? issue with allowMissingId
const opts = { uid, locale: entry.locale, status: 'draft', allowMissingId: true };
return transformData(entry, opts);
},
// Create the draft entry
(data) => createEntry({ ...params, locale: data.locale, data, status: 'draft' })
)(entry);
}

return {
create: createEntry,
delete: deleteEntry,
update: updateEntry,
publish: publishEntry,
discardDraft: discardDraftEntry,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const defaultLocale: AsyncTransform = async (contentType, params) => {
}

if (!params.locale) {
// TODO: Load default locale from db in i18n
return assoc('locale', await getDefaultLocale(), params);
}

Expand Down
42 changes: 7 additions & 35 deletions packages/core/core/src/services/document-service/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { createEntriesService } from './entries';
import { pickSelectionParams } from './params';
import { createDocumentId } from '../../utils/transform-content-types-to-models';
import { getDeepPopulate } from './utils/populate';
import { transformData } from './transform/data';
import { transformParamsToQuery } from './transform/query';
import { transformParamsDocumentId } from './transform/id-transform';

Expand Down Expand Up @@ -143,7 +142,7 @@ export const createContentTypeRepository: RepositoryFactoryMethod = (uid) => {
)
);

return { documentId: clonedEntries.at(0)?.documentId, versions: clonedEntries };
return { documentId, versions: clonedEntries };
}

async function update(opts = {} as any) {
Expand Down Expand Up @@ -224,7 +223,7 @@ export const createContentTypeRepository: RepositoryFactoryMethod = (uid) => {
});

// Get deep populate
const entriesToPublish = await strapi.db?.query(uid).findMany({
const draftsToPublish = await strapi.db?.query(uid).findMany({
where: {
...queryParams?.lookup,
documentId,
Expand All @@ -234,26 +233,11 @@ export const createContentTypeRepository: RepositoryFactoryMethod = (uid) => {
});

// Transform draft entry data and create published versions
const publishedEntries = await async.map(
entriesToPublish,
async.pipe(
// Updated at value is used to know if draft has been modified
// If both versions share the same value, it means the draft has not been modified
(draft) => assoc('updatedAt', draft.updatedAt, draft),
assoc('publishedAt', new Date()),
assoc('documentId', documentId),
omit('id'),
// Transform relations to target published versions
(entry) => {
const opts = { uid, locale: entry.locale, status: 'published', allowMissingId: true };
return transformData(entry, opts);
},
// Create the published entry
(data) => entries.create({ ...queryParams, data, locale: data.locale, status: 'published' })
)
const versions = await async.map(draftsToPublish, (draft: unknown) =>
entries.publish(draft, queryParams)
);

return { versions: publishedEntries };
return { versions };
}

async function unpublish(opts = {} as any) {
Expand Down Expand Up @@ -299,20 +283,8 @@ export const createContentTypeRepository: RepositoryFactoryMethod = (uid) => {
});

// Transform published entry data and create draft versions
const draftEntries = await async.map(
entriesToDraft,
async.pipe(
assoc('publishedAt', null),
assoc('documentId', documentId),
omit('id'),
// Transform relations to target draft versions
(entry) => {
const opts = { uid, locale: entry.locale, status: 'draft', allowMissingId: true };
return transformData(entry, opts);
},
// Create the draft entry
(data) => entries.create({ ...queryParams, locale: data.locale, data, status: 'draft' })
)
const draftEntries = await async.map(entriesToDraft, (entry: any) =>
entries.discardDraft(entry, queryParams)
);

return { versions: draftEntries };
Expand Down

0 comments on commit 73062d2

Please sign in to comment.