Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: _updatedAt data type #125

Merged
merged 3 commits into from
Mar 21, 2024
Merged
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
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