fix: ensures old data from arrays is not persisted

This commit is contained in:
James
2022-07-06 10:52:52 -07:00
parent 91e33d1c1c
commit d9ef803d20
5 changed files with 58 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ const { serverURL: url } = getConfig();
let token = null;
let headers = null;
let localizedPostID;
let localizedPost;
let relationshipBID;
const localizedPostTitle = 'title';
const englishPostDesc = 'english description';
@@ -91,7 +91,7 @@ describe('Collections - REST', () => {
expect(data.doc.createdAt).toStrictEqual(expect.stringMatching(timestampRegex));
expect(data.doc.updatedAt).toStrictEqual(expect.stringMatching(timestampRegex));
localizedPostID = data.doc.id;
localizedPost = data.doc;
});
it('should add id to array items', async () => {
@@ -170,7 +170,7 @@ describe('Collections - REST', () => {
});
it('should allow a Spanish locale to be added to an existing post', async () => {
const response = await fetch(`${url}/api/localized-posts/${localizedPostID}?locale=es`, {
const response = await fetch(`${url}/api/localized-posts/${localizedPost.id}?locale=es`, {
body: JSON.stringify({
title: 'title in spanish',
description: spanishPostDesc,
@@ -183,11 +183,13 @@ describe('Collections - REST', () => {
},
nonLocalizedArray: [
{
id: localizedPost.nonLocalizedArray[0].id,
localizedEmbeddedText: 'spanish',
},
],
richTextBlocks: [
{
id: localizedPost.richTextBlocks[0].id,
blockType: 'richTextBlock',
blockName: 'Test Block Name',
content: [
@@ -216,7 +218,7 @@ describe('Collections - REST', () => {
describe('Read', () => {
describe('Localized', () => {
it('should allow a localized post to be retrieved in unspecified locale, defaulting to English', async () => {
const response = await fetch(`${url}/api/localized-posts/${localizedPostID}`);
const response = await fetch(`${url}/api/localized-posts/${localizedPost.id}`);
const data = await response.json();
expect(response.status).toBe(200);
@@ -228,7 +230,7 @@ describe('Collections - REST', () => {
});
it('should allow a localized post to be retrieved in specified English locale', async () => {
const response = await fetch(`${url}/api/localized-posts/${localizedPostID}?locale=en`);
const response = await fetch(`${url}/api/localized-posts/${localizedPost.id}?locale=en`);
const data = await response.json();
expect(response.status).toBe(200);
@@ -240,7 +242,7 @@ describe('Collections - REST', () => {
});
it('should allow a localized post to be retrieved in Spanish', async () => {
const response = await fetch(`${url}/api/localized-posts/${localizedPostID}?locale=es`);
const response = await fetch(`${url}/api/localized-posts/${localizedPost.id}?locale=es`);
const data = await response.json();
expect(response.status).toBe(200);
@@ -252,7 +254,7 @@ describe('Collections - REST', () => {
});
it('should allow a localized post to be retrieved in all locales', async () => {
const response = await fetch(`${url}/api/localized-posts/${localizedPostID}?locale=all`);
const response = await fetch(`${url}/api/localized-posts/${localizedPost.id}?locale=all`);
const data = await response.json();
expect(response.status).toBe(200);
@@ -417,7 +419,7 @@ describe('Collections - REST', () => {
postLocalizedMultiple: [
{
relationTo: 'localized-posts',
value: localizedPostID,
value: localizedPost.id,
},
],
}),
@@ -442,7 +444,7 @@ describe('Collections - REST', () => {
it('should allow querying by a localized nested relationship property', async () => {
const res = await fetch(`${url}/api/relationship-a`, {
body: JSON.stringify({
LocalizedPost: [localizedPostID],
LocalizedPost: [localizedPost.id],
}),
headers,
method: 'post',
@@ -496,7 +498,7 @@ describe('Collections - REST', () => {
title: 'awefjlaiwejfalweiijfaew',
nonLocalizedRelationToMany: {
relationTo: 'localized-posts',
value: localizedPostID,
value: localizedPost.id,
},
}),
headers,
@@ -507,7 +509,7 @@ describe('Collections - REST', () => {
relationshipBID = relationshipB.doc.id;
const queryRes = await fetch(
`${url}/api/relationship-b?where[nonLocalizedRelationToMany.value][equals]=${localizedPostID}`,
`${url}/api/relationship-b?where[nonLocalizedRelationToMany.value][equals]=${localizedPost.id}`,
);
const data = await queryRes.json();
expect(data.docs).toHaveLength(1);

View File

@@ -0,0 +1,14 @@
/**
* If there is an incoming row id,
* and it matches the existing sibling doc id,
* this is an existing row, so it should be merged.
* Otherwise, return an empty object.
*/
export const getExistingRowDoc = (incomingRow: Record<string, unknown>, existingRow?: Record<string, unknown>): Record<string, unknown> => {
if (incomingRow.id && incomingRow.id === existingRow?.id) {
return existingRow;
}
return {};
};

View File

@@ -5,6 +5,7 @@ import { Operation } from '../../../types';
import { PayloadRequest } from '../../../express/types';
import getValueWithDefault from '../../getDefaultValue';
import { traverseFields } from './traverseFields';
import { getExistingRowDoc } from './getExistingRowDoc';
type Args = {
data: Record<string, unknown>
@@ -217,8 +218,8 @@ export const promise = async ({
promises,
req,
siblingData: row,
siblingDoc: siblingDoc[field.name]?.[i] || {},
siblingDocWithLocales: siblingDocWithLocales[field.name]?.[i] || {},
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]),
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]),
skipValidation: skipValidationFromHere,
});
});
@@ -247,8 +248,8 @@ export const promise = async ({
promises,
req,
siblingData: row,
siblingDoc: siblingDoc[field.name]?.[i] || {},
siblingDocWithLocales: siblingDocWithLocales[field.name]?.[i] || {},
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]),
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]),
skipValidation: skipValidationFromHere,
});
}