Skip to content

Commit

Permalink
Merge pull request #125 from konecty/fix/_updatedAt-data-type
Browse files Browse the repository at this point in the history
Fix: _updatedAt data type
  • Loading branch information
silveirado authored Mar 21, 2024
2 parents daf2407 + a9a3c15 commit 25dca65
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 6 deletions.
155 changes: 155 additions & 0 deletions __test__/metadata/Product/updateProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,161 @@ describe('Update Product', () => {
expect(data.data?.[0].name).to.be.equal('Test Updated');
});

it('Update Product with other type of date', async () => {
// Arrange
const product = await createProductHelper(authId);

const requestFields = {
data: {
name: 'Test Updated',
},
ids: [
{
_id: product?._id,
_updatedAt: product?._updatedAt,
},
],
};

// Act
const data = (await fetch(`http://127.0.0.1:3000/rest/data/Product`, {
method: 'PUT',
headers: {
Cookie: `_authTokenId=${authId}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestFields),
}).then(res => res.json())) as KonectyResponse;

// Assert
expect(data.success).to.be.equal(true);
expect(data.data?.[0].name).to.be.equal('Test Updated');
});

it('should not update Product without _updatedAt on id', async () => {
// Arrange
const product = await createProductHelper(authId);

const requestFields = {
data: {
name: 'Test Updated',
},
ids: [
{
_id: product?._id,
_updatedAt: undefined,
},
],
};

// Act
const data = (await fetch(`http://127.0.0.1:3000/rest/data/Product`, {
method: 'PUT',
headers: {
Cookie: `_authTokenId=${authId}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestFields),
}).then(res => res.json())) as KonectyResponse;

// Assert
expect(data.success).to.be.equal(false);
expect(data.errors?.[0]?.message).to.be.equal('[Product] Each id must contain an string field named _id an date field named _updatedAt');
});

it('should not update Product with invalid _updatedAt on id', async () => {
// Arrange
const product = await createProductHelper(authId);

const requestFields = {
data: {
name: 'Test Updated',
},
ids: [
{
_id: product?._id,
_updatedAt: 123,
},
],
};

// Act
const data = (await fetch(`http://127.0.0.1:3000/rest/data/Product`, {
method: 'PUT',
headers: {
Cookie: `_authTokenId=${authId}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestFields),
}).then(res => res.json())) as KonectyResponse;

// Assert
expect(data.success).to.be.equal(false);
expect(data.errors?.[0]?.message).to.be.equal('[Product] Each id must contain an string field named _id an date field named _updatedAt');
});

it('should not update Product with invalid _updatedAt on id', async () => {
// Arrange
const product = await createProductHelper(authId);

const requestFields = {
data: {
name: 'Test Updated',
},
ids: [
{
_id: product?._id,
_updatedAt: true,
},
],
};

// Act
const data = (await fetch(`http://127.0.0.1:3000/rest/data/Product`, {
method: 'PUT',
headers: {
Cookie: `_authTokenId=${authId}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestFields),
}).then(res => res.json())) as KonectyResponse;

// Assert
expect(data.success).to.be.equal(false);
expect(data.errors?.[0]?.message).to.be.equal('[Product] Each id must contain an string field named _id an date field named _updatedAt');
});

it('Update Product with other type of date 2', async () => {
// Arrange
const product = await createProductHelper(authId);

const requestFields = {
data: {
name: 'Test Updated',
},
ids: [
{
_id: product?._id,
_updatedAt: new Date(product?._updatedAt as string),
},
],
};

// Act
const data = (await fetch(`http://127.0.0.1:3000/rest/data/Product`, {
method: 'PUT',
headers: {
Cookie: `_authTokenId=${authId}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestFields),
}).then(res => res.json())) as KonectyResponse;

// Assert
expect(data.success).to.be.equal(true);
expect(data.data?.[0].name).to.be.equal('Test Updated');
});

it('Update Product and test scriptBeforeValidation', async () => {
// Arrange
const product = await createProductHelper(authId);
Expand Down
1 change: 0 additions & 1 deletion __test__/metadata/User/updateUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('Update User', () => {
'Content-Type': 'application/json',
},
}).then(res => res.json())) as KonectyResponse;
console.log(data);

// Assert
expect(data.success).to.be.equal(true);
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const jestConfig: JestConfigWithTsJest = {
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src' }),
watchPathIgnorePatterns: ['globalConfig'],
testPathIgnorePatterns: ['/node_modules/', '/coverage/', '/build/'],
testPathIgnorePatterns: ['/node_modules/', '/coverage/', '/build/', '/dist/'],
};

export default jestConfig;
20 changes: 16 additions & 4 deletions src/imports/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import set from 'lodash/set';
import size from 'lodash/size';
import tail from 'lodash/tail';
import unset from 'lodash/unset';
import isDate from 'lodash/isDate';
import words from 'lodash/words';

import { MetaObject } from '@imports/model/MetaObject';
Expand Down Expand Up @@ -1305,12 +1306,16 @@ export async function update({ authTokenId, document, data, contextUser, tracing
return false;
}
if (metaObject.ignoreUpdatedAt !== true) {
if (isObject(id._updatedAt) === false) {
if (isObject(id._updatedAt) === true) {
if (isString(id._updatedAt.$date) === true || isDate(id._updatedAt.$date) === true) {
return true;
}
return false;
}
if (isString(id._updatedAt.$date) === false) {
return false;
if (isString(id._updatedAt) === true || isDate(id._updatedAt) === true) {
return true;
}
return false;
}
return true;
});
Expand Down Expand Up @@ -1767,9 +1772,16 @@ export async function deleteData({ authTokenId, document, data, contextUser }) {
return false;
}
if (metaObject.ignoreUpdatedAt !== true) {
if (id?._updatedAt == null || isObject(id._updatedAt) === false || isString(id._updatedAt.$date) === false) {
if (isObject(id._updatedAt) === true) {
if (isString(id._updatedAt.$date) === true || isDate(id._updatedAt.$date) === true) {
return true;
}
return false;
}
if (isString(id._updatedAt) === true || isDate(id._updatedAt) === true) {
return true;
}
return false;
}
return true;
});
Expand Down
7 changes: 7 additions & 0 deletions src/imports/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default function initializeInstrumentation() {
url: OTEL_URL,
});
}
if (process.env.NODE_ENV === 'test') {
return undefined;
}

return new ConsoleSpanExporter();
};

Expand All @@ -35,6 +39,9 @@ export default function initializeInstrumentation() {
endpoint: PROMETHEUS_URL,
});
}
if (process.env.NODE_ENV === 'test') {
return undefined;
}
return new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
});
Expand Down

0 comments on commit 25dca65

Please sign in to comment.