From f44f3d54d1f8a8db7d706be95d4be236e6fd4d44 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Thu, 2 Jun 2022 15:11:31 +0200 Subject: [PATCH 1/6] feat: Call should end on endCycle instead of endCall date --- src/asyncJobs/jobs/checkCallsEnded.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/asyncJobs/jobs/checkCallsEnded.ts b/src/asyncJobs/jobs/checkCallsEnded.ts index 59631b4ae..a16bb20de 100644 --- a/src/asyncJobs/jobs/checkCallsEnded.ts +++ b/src/asyncJobs/jobs/checkCallsEnded.ts @@ -15,8 +15,9 @@ const checkCallsEnded = async (dataSource: CallDataSource) => { const currentDate = new Date(); + // NOTE: Call should end on "endCycle" date not on endCall. const callsThatShouldEnd = notEndedCalls.filter( - (notEndedCall) => notEndedCall.endCall.getTime() <= currentDate.getTime() + (notEndedCall) => notEndedCall.endCycle.getTime() <= currentDate.getTime() ); const updatedCalls = []; From cd0afcce29d6f35b5c694bf1e25acc58b248887d Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Fri, 10 Jun 2022 16:29:43 +0200 Subject: [PATCH 2/6] revert some changes and fix the call end by cycle date --- src/asyncJobs/jobs/checkCallsEnded.ts | 3 +-- src/datasources/postgres/CallDataSource.ts | 4 ++-- src/resolvers/types/Call.ts | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/asyncJobs/jobs/checkCallsEnded.ts b/src/asyncJobs/jobs/checkCallsEnded.ts index a16bb20de..59631b4ae 100644 --- a/src/asyncJobs/jobs/checkCallsEnded.ts +++ b/src/asyncJobs/jobs/checkCallsEnded.ts @@ -15,9 +15,8 @@ const checkCallsEnded = async (dataSource: CallDataSource) => { const currentDate = new Date(); - // NOTE: Call should end on "endCycle" date not on endCall. const callsThatShouldEnd = notEndedCalls.filter( - (notEndedCall) => notEndedCall.endCycle.getTime() <= currentDate.getTime() + (notEndedCall) => notEndedCall.endCall.getTime() <= currentDate.getTime() ); const updatedCalls = []; diff --git a/src/datasources/postgres/CallDataSource.ts b/src/datasources/postgres/CallDataSource.ts index a23f49423..10c8b79c0 100644 --- a/src/datasources/postgres/CallDataSource.ts +++ b/src/datasources/postgres/CallDataSource.ts @@ -51,12 +51,12 @@ export default class PostgresCallDataSource implements CallDataSource { const currentDate = new Date().toISOString(); query .where('start_call', '<=', currentDate) - .andWhere('end_call', '>=', currentDate); + .andWhere('end_cycle', '>=', currentDate); } else if (filter?.isActive === false) { const currentDate = new Date().toISOString(); query .where('start_call', '>=', currentDate) - .orWhere('end_call', '<=', currentDate); + .orWhere('end_cycle', '<=', currentDate); } if (filter?.isEnded === true) { diff --git a/src/resolvers/types/Call.ts b/src/resolvers/types/Call.ts index 608d04ea1..99f0359eb 100644 --- a/src/resolvers/types/Call.ts +++ b/src/resolvers/types/Call.ts @@ -122,9 +122,9 @@ export class CallInstrumentsResolver { isActive(@Root() call: Call): boolean { const now = new Date(); const startCall = new Date(call.startCall); - const endCall = new Date(call.endCall); + const endCycle = new Date(call.endCycle); - return startCall <= now && endCall >= now; + return startCall <= now && endCycle >= now; } } From 69d256254b3e905807ef7228ea24be8948568871 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Tue, 21 Jun 2022 16:33:46 +0200 Subject: [PATCH 3/6] feat: Change isActive logic on the call with introducing new flag in the database --- db_patches/0124_AddIsActiveCallFlag.sql | 12 ++ package-lock.json | 121 ++++++------------ package.json | 4 +- src/datasources/mockups/CallDataSource.ts | 9 +- src/datasources/postgres/CallDataSource.ts | 11 +- src/datasources/postgres/records.ts | 4 +- src/models/Call.ts | 3 +- src/resolvers/mutations/UpdateCallMutation.ts | 3 + src/resolvers/types/Call.ts | 12 +- 9 files changed, 72 insertions(+), 107 deletions(-) create mode 100644 db_patches/0124_AddIsActiveCallFlag.sql diff --git a/db_patches/0124_AddIsActiveCallFlag.sql b/db_patches/0124_AddIsActiveCallFlag.sql new file mode 100644 index 000000000..2d67be111 --- /dev/null +++ b/db_patches/0124_AddIsActiveCallFlag.sql @@ -0,0 +1,12 @@ +DO +$$ +BEGIN + IF register_patch('AddIsActiveCallFlag.sql', 'Martin Trajanovski', 'Add is_active flag to the calls table', '2022-06-21') THEN + + ALTER TABLE "call" + ADD COLUMN is_active boolean DEFAULT true; + + END IF; +END; +$$ +LANGUAGE plpgsql; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 00ede0877..c1726a877 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,9 +14,9 @@ "@user-office-software/duo-logger": "^2.0.2", "@user-office-software/duo-message-broker": "^1.3.0", "@user-office-software/duo-validation": "^3.2.0", - "apollo-graphql": "^0.9.5", + "apollo-graphql": "^0.9.7", "apollo-server-core": "^3.9.0", - "apollo-server-express": "^3.6.3", + "apollo-server-express": "^3.9.0", "await-to-js": "^2.1.1", "axios": "^0.26.0", "bcryptjs": "^2.4.3", @@ -1770,9 +1770,9 @@ } }, "node_modules/@types/express-serve-static-core": { - "version": "4.17.28", - "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.28.tgz", - "integrity": "sha512-P1BJAEAW3E2DJUlkgq4tOL3RyMunoWXqbSCygWo5ZIWTjUgN1YnaXWW4VWl/oc8vs/XoYibEGBKP0uZyF4AHig==", + "version": "4.17.29", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.29.tgz", + "integrity": "sha512-uMd++6dMKS32EOuw1Uli3e3BPgdLIXmezcfHv7N4c1s3gkhikBplORPpMq3fuWkxncZN1reb16d5n8yhQ80x7Q==", "dependencies": { "@types/node": "*", "@types/qs": "*", @@ -2955,9 +2955,9 @@ } }, "node_modules/apollo-graphql": { - "version": "0.9.5", - "resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.9.5.tgz", - "integrity": "sha512-RGt5k2JeBqrmnwRM0VOgWFiGKlGJMfmiif/4JvdaEqhMJ+xqe/9cfDYzXfn33ke2eWixsAbjEbRfy8XbaN9nTw==", + "version": "0.9.7", + "resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.9.7.tgz", + "integrity": "sha512-bezL9ItUWUGHTm1bI/XzIgiiZbhXpsC7uxk4UxFPmcVJwJsDc3ayZ99oXxAaK+3Rbg/IoqrHckA6CwmkCsbaSA==", "dependencies": { "core-js-pure": "^3.10.2", "lodash.sortby": "^4.7.0", @@ -2978,17 +2978,6 @@ "@apollo/protobufjs": "1.2.2" } }, - "node_modules/apollo-server-caching": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-3.3.0.tgz", - "integrity": "sha512-Wgcb0ArjZ5DjQ7ID+tvxUcZ7Yxdbk5l1MxZL8D8gkyjooOkhPNzjRVQ7ubPoXqO54PrOMOTm1ejVhsF+AfIirQ==", - "dependencies": { - "lru-cache": "^6.0.0" - }, - "engines": { - "node": ">=12.0" - } - }, "node_modules/apollo-server-core": { "version": "3.9.0", "resolved": "https://registry.npmjs.org/apollo-server-core/-/apollo-server-core-3.9.0.tgz", @@ -3163,23 +3152,6 @@ "graphql": "^15.3.0 || ^16.0.0" } }, - "node_modules/apollo-server-core/node_modules/apollo-server-types": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.6.1.tgz", - "integrity": "sha512-XOPlBlRdwP00PrG03OffGGWuzyei+J9t1rAnvyHsSdP0JCgQWigHJfvL1N9Bhgi4UTjl9JadKOJh1znLNlqIFQ==", - "dependencies": { - "@apollo/utils.keyvaluecache": "^1.0.1", - "@apollo/utils.logger": "^1.0.0", - "apollo-reporting-protobuf": "^3.3.1", - "apollo-server-env": "^4.2.1" - }, - "engines": { - "node": ">=12.0" - }, - "peerDependencies": { - "graphql": "^15.3.0 || ^16.0.0" - } - }, "node_modules/apollo-server-core/node_modules/whatwg-mimetype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", @@ -3200,18 +3172,18 @@ } }, "node_modules/apollo-server-express": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/apollo-server-express/-/apollo-server-express-3.6.3.tgz", - "integrity": "sha512-3CjahZ+n+1T7pHH1qW1B6Ns0BzwOMeupAp2u0+M8ruOmE/e7VKn0OSOQQckZ8Z2AcWxWeno9K89fIv3PoSYgYA==", + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/apollo-server-express/-/apollo-server-express-3.9.0.tgz", + "integrity": "sha512-scSeHy9iB7W3OiF3uLQEzad9Jm9tEfDF8ACsJb2P+xX69uqg6zizsrQvj3qRhazCO7FKMcMu9zQFR0hy7zKbUA==", "dependencies": { "@types/accepts": "^1.3.5", "@types/body-parser": "1.19.2", "@types/cors": "2.8.12", "@types/express": "4.17.13", - "@types/express-serve-static-core": "4.17.28", + "@types/express-serve-static-core": "4.17.29", "accepts": "^1.3.5", - "apollo-server-core": "^3.6.3", - "apollo-server-types": "^3.5.1", + "apollo-server-core": "^3.9.0", + "apollo-server-types": "^3.6.1", "body-parser": "^1.19.0", "cors": "^2.8.5", "parseurl": "^1.3.3" @@ -3225,12 +3197,13 @@ } }, "node_modules/apollo-server-types": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.5.1.tgz", - "integrity": "sha512-zG7xLl4mmHuZMAYOfjWKHY/IC/GgIkJ3HnYuR7FRrnPpRA9Yt5Kf1M1rjm1Esuqzpb/dt8pM7cX40QaIQObCYQ==", + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.6.1.tgz", + "integrity": "sha512-XOPlBlRdwP00PrG03OffGGWuzyei+J9t1rAnvyHsSdP0JCgQWigHJfvL1N9Bhgi4UTjl9JadKOJh1znLNlqIFQ==", "dependencies": { - "apollo-reporting-protobuf": "^3.3.0", - "apollo-server-caching": "^3.3.0", + "@apollo/utils.keyvaluecache": "^1.0.1", + "@apollo/utils.logger": "^1.0.0", + "apollo-reporting-protobuf": "^3.3.1", "apollo-server-env": "^4.2.1" }, "engines": { @@ -14146,9 +14119,9 @@ } }, "@types/express-serve-static-core": { - "version": "4.17.28", - "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.28.tgz", - "integrity": "sha512-P1BJAEAW3E2DJUlkgq4tOL3RyMunoWXqbSCygWo5ZIWTjUgN1YnaXWW4VWl/oc8vs/XoYibEGBKP0uZyF4AHig==", + "version": "4.17.29", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.29.tgz", + "integrity": "sha512-uMd++6dMKS32EOuw1Uli3e3BPgdLIXmezcfHv7N4c1s3gkhikBplORPpMq3fuWkxncZN1reb16d5n8yhQ80x7Q==", "requires": { "@types/node": "*", "@types/qs": "*", @@ -15026,9 +14999,9 @@ } }, "apollo-graphql": { - "version": "0.9.5", - "resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.9.5.tgz", - "integrity": "sha512-RGt5k2JeBqrmnwRM0VOgWFiGKlGJMfmiif/4JvdaEqhMJ+xqe/9cfDYzXfn33ke2eWixsAbjEbRfy8XbaN9nTw==", + "version": "0.9.7", + "resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.9.7.tgz", + "integrity": "sha512-bezL9ItUWUGHTm1bI/XzIgiiZbhXpsC7uxk4UxFPmcVJwJsDc3ayZ99oXxAaK+3Rbg/IoqrHckA6CwmkCsbaSA==", "requires": { "core-js-pure": "^3.10.2", "lodash.sortby": "^4.7.0", @@ -15043,14 +15016,6 @@ "@apollo/protobufjs": "1.2.2" } }, - "apollo-server-caching": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-3.3.0.tgz", - "integrity": "sha512-Wgcb0ArjZ5DjQ7ID+tvxUcZ7Yxdbk5l1MxZL8D8gkyjooOkhPNzjRVQ7ubPoXqO54PrOMOTm1ejVhsF+AfIirQ==", - "requires": { - "lru-cache": "^6.0.0" - } - }, "apollo-server-core": { "version": "3.9.0", "resolved": "https://registry.npmjs.org/apollo-server-core/-/apollo-server-core-3.9.0.tgz", @@ -15168,17 +15133,6 @@ "apollo-server-types": "^3.6.1" } }, - "apollo-server-types": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.6.1.tgz", - "integrity": "sha512-XOPlBlRdwP00PrG03OffGGWuzyei+J9t1rAnvyHsSdP0JCgQWigHJfvL1N9Bhgi4UTjl9JadKOJh1znLNlqIFQ==", - "requires": { - "@apollo/utils.keyvaluecache": "^1.0.1", - "@apollo/utils.logger": "^1.0.0", - "apollo-reporting-protobuf": "^3.3.1", - "apollo-server-env": "^4.2.1" - } - }, "whatwg-mimetype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", @@ -15195,30 +15149,31 @@ } }, "apollo-server-express": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/apollo-server-express/-/apollo-server-express-3.6.3.tgz", - "integrity": "sha512-3CjahZ+n+1T7pHH1qW1B6Ns0BzwOMeupAp2u0+M8ruOmE/e7VKn0OSOQQckZ8Z2AcWxWeno9K89fIv3PoSYgYA==", + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/apollo-server-express/-/apollo-server-express-3.9.0.tgz", + "integrity": "sha512-scSeHy9iB7W3OiF3uLQEzad9Jm9tEfDF8ACsJb2P+xX69uqg6zizsrQvj3qRhazCO7FKMcMu9zQFR0hy7zKbUA==", "requires": { "@types/accepts": "^1.3.5", "@types/body-parser": "1.19.2", "@types/cors": "2.8.12", "@types/express": "4.17.13", - "@types/express-serve-static-core": "4.17.28", + "@types/express-serve-static-core": "4.17.29", "accepts": "^1.3.5", - "apollo-server-core": "^3.6.3", - "apollo-server-types": "^3.5.1", + "apollo-server-core": "^3.9.0", + "apollo-server-types": "^3.6.1", "body-parser": "^1.19.0", "cors": "^2.8.5", "parseurl": "^1.3.3" } }, "apollo-server-types": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.5.1.tgz", - "integrity": "sha512-zG7xLl4mmHuZMAYOfjWKHY/IC/GgIkJ3HnYuR7FRrnPpRA9Yt5Kf1M1rjm1Esuqzpb/dt8pM7cX40QaIQObCYQ==", + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-3.6.1.tgz", + "integrity": "sha512-XOPlBlRdwP00PrG03OffGGWuzyei+J9t1rAnvyHsSdP0JCgQWigHJfvL1N9Bhgi4UTjl9JadKOJh1znLNlqIFQ==", "requires": { - "apollo-reporting-protobuf": "^3.3.0", - "apollo-server-caching": "^3.3.0", + "@apollo/utils.keyvaluecache": "^1.0.1", + "@apollo/utils.logger": "^1.0.0", + "apollo-reporting-protobuf": "^3.3.1", "apollo-server-env": "^4.2.1" } }, diff --git a/package.json b/package.json index 50b1e192c..a2a51658f 100644 --- a/package.json +++ b/package.json @@ -44,9 +44,9 @@ "@user-office-software/duo-logger": "^2.0.2", "@user-office-software/duo-message-broker": "^1.3.0", "@user-office-software/duo-validation": "^3.2.0", - "apollo-graphql": "^0.9.5", + "apollo-graphql": "^0.9.7", "apollo-server-core": "^3.9.0", - "apollo-server-express": "^3.6.3", + "apollo-server-express": "^3.9.0", "await-to-js": "^2.1.1", "axios": "^0.26.0", "bcryptjs": "^2.4.3", diff --git a/src/datasources/mockups/CallDataSource.ts b/src/datasources/mockups/CallDataSource.ts index 0b32fa260..ea543d2b6 100644 --- a/src/datasources/mockups/CallDataSource.ts +++ b/src/datasources/mockups/CallDataSource.ts @@ -35,7 +35,8 @@ export const dummyCallFactory = (values?: Partial) => { values?.esiTemplateId || 2, values?.allocationTimeUnit || AllocationTimeUnits.Day, values?.title || 'Title', - values?.description || 'Description' + values?.description || 'Description', + values?.isActive || true ); }; @@ -65,7 +66,8 @@ export const dummyCall = new Call( 2, AllocationTimeUnits.Day, '', - '' + '', + true ); export const anotherDummyCall = new Call( @@ -94,7 +96,8 @@ export const anotherDummyCall = new Call( 2, AllocationTimeUnits.Day, '', - '' + '', + true ); export const dummyCalls = [dummyCall, anotherDummyCall]; diff --git a/src/datasources/postgres/CallDataSource.ts b/src/datasources/postgres/CallDataSource.ts index 10c8b79c0..af3c8320c 100644 --- a/src/datasources/postgres/CallDataSource.ts +++ b/src/datasources/postgres/CallDataSource.ts @@ -48,15 +48,9 @@ export default class PostgresCallDataSource implements CallDataSource { // if filter is explicitly set to true or false if (filter?.isActive === true) { - const currentDate = new Date().toISOString(); - query - .where('start_call', '<=', currentDate) - .andWhere('end_cycle', '>=', currentDate); + query.where('is_active', true); } else if (filter?.isActive === false) { - const currentDate = new Date().toISOString(); - query - .where('start_call', '>=', currentDate) - .orWhere('end_cycle', '<=', currentDate); + query.where('is_active', false); } if (filter?.isEnded === true) { @@ -190,6 +184,7 @@ export default class PostgresCallDataSource implements CallDataSource { allocation_time_unit: args.allocationTimeUnit, title: args.title, description: args.description, + is_active: args.isActive, }, ['*'] ) diff --git a/src/datasources/postgres/records.ts b/src/datasources/postgres/records.ts index 975e5db1f..474c3db8a 100644 --- a/src/datasources/postgres/records.ts +++ b/src/datasources/postgres/records.ts @@ -289,6 +289,7 @@ export interface CallRecord { readonly allocation_time_unit: AllocationTimeUnits; readonly title: string; readonly description: string; + readonly is_active: boolean; } export interface PageTextRecord { @@ -844,7 +845,8 @@ export const createCallObject = (call: CallRecord) => { call.esi_template_id, call.allocation_time_unit, call.title, - call.description + call.description, + call.is_active ); }; diff --git a/src/models/Call.ts b/src/models/Call.ts index 085ca4116..258f44ecf 100644 --- a/src/models/Call.ts +++ b/src/models/Call.ts @@ -25,7 +25,8 @@ export class Call { public esiTemplateId: number | undefined, public allocationTimeUnit: AllocationTimeUnits, public title: string, - public description: string + public description: string, + public isActive: boolean ) {} } diff --git a/src/resolvers/mutations/UpdateCallMutation.ts b/src/resolvers/mutations/UpdateCallMutation.ts index 39fc92c92..e3aa74a84 100644 --- a/src/resolvers/mutations/UpdateCallMutation.ts +++ b/src/resolvers/mutations/UpdateCallMutation.ts @@ -92,6 +92,9 @@ export class UpdateCallInput { @Field({ nullable: true }) public description: string; + + @Field(() => Boolean, { nullable: true }) + public isActive?: boolean; } @InputType() diff --git a/src/resolvers/types/Call.ts b/src/resolvers/types/Call.ts index 99f0359eb..3e1af4190 100644 --- a/src/resolvers/types/Call.ts +++ b/src/resolvers/types/Call.ts @@ -88,6 +88,9 @@ export class Call implements Partial { @Field({ nullable: true }) public description: string; + + @Field(() => Boolean) + public isActive: boolean; } @Resolver(() => Call) @@ -117,15 +120,6 @@ export class CallInstrumentsResolver { async proposalCount(@Root() call: Call, @Ctx() context: ResolverContext) { return context.queries.proposal.dataSource.getCount(call.id); } - - @FieldResolver(() => Boolean) - isActive(@Root() call: Call): boolean { - const now = new Date(); - const startCall = new Date(call.startCall); - const endCycle = new Date(call.endCycle); - - return startCall <= now && endCycle >= now; - } } // eslint-disable-next-line @typescript-eslint/no-explicit-any From dec5e270afee7302ceb92272a55c43d85ae792ab Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Wed, 22 Jun 2022 09:34:55 +0200 Subject: [PATCH 4/6] fix unit tests --- src/mutations/CallMutations.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mutations/CallMutations.spec.ts b/src/mutations/CallMutations.spec.ts index e85a1425b..629762951 100644 --- a/src/mutations/CallMutations.spec.ts +++ b/src/mutations/CallMutations.spec.ts @@ -142,6 +142,7 @@ describe('Test Call Mutations', () => { callReviewEnded: false, callSEPReviewEnded: false, templateId: 1, + isActive: true, }); }); @@ -198,6 +199,7 @@ describe('Test Call Mutations', () => { templateId: 1, title: 'Title', description: 'Description', + isActive: true, }; return expect( From 09e02e87f49d94448a75ffeacbdab7b587e05796 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Wed, 22 Jun 2022 17:30:14 +0200 Subject: [PATCH 5/6] Fix the call ended comparisson and checks --- src/auth/ProposalAuthorization.ts | 8 ++++---- src/datasources/CallDataSource.ts | 2 +- src/datasources/mockups/CallDataSource.ts | 2 +- src/datasources/postgres/CallDataSource.ts | 18 ++++++++++++++---- src/mutations/ProposalMutations.ts | 10 ++++------ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/auth/ProposalAuthorization.ts b/src/auth/ProposalAuthorization.ts index 480787ec1..4d5facaae 100644 --- a/src/auth/ProposalAuthorization.ts +++ b/src/auth/ProposalAuthorization.ts @@ -196,7 +196,7 @@ export class ProposalAuthorization { private async isProposalEditable(proposal: Proposal): Promise { const callId = proposal.callId; - const isCallActive = await this.callDataSource.checkActiveCall(callId); + const isCallEnded = await this.callDataSource.isCallEnded(callId); const proposalStatus = ( await this.proposalSettingsDataSource.getProposalStatus(proposal.statusId) )?.shortCode; @@ -205,10 +205,10 @@ export class ProposalAuthorization { return true; } - if (isCallActive) { - return proposalStatus === ProposalStatusDefaultShortCodes.DRAFT; - } else { + if (isCallEnded) { return false; + } else { + return proposalStatus === ProposalStatusDefaultShortCodes.DRAFT; } } diff --git a/src/datasources/CallDataSource.ts b/src/datasources/CallDataSource.ts index 94dba38e1..c0b1831c9 100644 --- a/src/datasources/CallDataSource.ts +++ b/src/datasources/CallDataSource.ts @@ -18,5 +18,5 @@ export interface CallDataSource { args: RemoveAssignedInstrumentFromCallInput ): Promise; getCallsByInstrumentScientist(scientistId: number): Promise; - checkActiveCall(callId: number): Promise; + isCallEnded(callId: number): Promise; } diff --git a/src/datasources/mockups/CallDataSource.ts b/src/datasources/mockups/CallDataSource.ts index ea543d2b6..02968132b 100644 --- a/src/datasources/mockups/CallDataSource.ts +++ b/src/datasources/mockups/CallDataSource.ts @@ -156,7 +156,7 @@ export class CallDataSourceMock implements CallDataSource { return dummyCalls; } - async checkActiveCall(callId: number): Promise { + async isCallEnded(callId: number): Promise { return callId === 1; } } diff --git a/src/datasources/postgres/CallDataSource.ts b/src/datasources/postgres/CallDataSource.ts index af3c8320c..9cc7ba8d0 100644 --- a/src/datasources/postgres/CallDataSource.ts +++ b/src/datasources/postgres/CallDataSource.ts @@ -53,10 +53,20 @@ export default class PostgresCallDataSource implements CallDataSource { query.where('is_active', false); } + /** + * NOTE: We are comparing dates instead of using the call_ended flag, + * because the flag is set once per hour and we could have a gap. + * TODO: Maybe there is a need to use the timezone setting here but not quite sure about it. Discussion is needed here! + */ + const currentDate = new Date().toISOString(); if (filter?.isEnded === true) { - query.where('call_ended', true); + query + .where('start_call', '>=', currentDate) + .andWhere('end_call', '<=', currentDate); } else if (filter?.isEnded === false) { - query.where('call_ended', false); + query + .where('start_call', '<=', currentDate) + .andWhere('end_call', '>=', currentDate); } if (filter?.isReviewEnded === true) { @@ -269,7 +279,7 @@ export default class PostgresCallDataSource implements CallDataSource { return records.map(createCallObject); } - public async checkActiveCall(callId: number): Promise { + public async isCallEnded(callId: number): Promise { const currentDate = new Date().toISOString(); return database @@ -279,6 +289,6 @@ export default class PostgresCallDataSource implements CallDataSource { .andWhere('end_call', '>=', currentDate) .andWhere('call_id', '=', callId) .first() - .then((call: CallRecord) => (call ? true : false)); + .then((call: CallRecord) => (call ? false : true)); } } diff --git a/src/mutations/ProposalMutations.ts b/src/mutations/ProposalMutations.ts index f489bad58..c1d544edd 100644 --- a/src/mutations/ProposalMutations.ts +++ b/src/mutations/ProposalMutations.ts @@ -67,7 +67,7 @@ export default class ProposalMutations { { callId }: { callId: number } ): Promise { // Check if there is an open call - if (!(await this.callDataSource.checkActiveCall(callId))) { + if (await this.callDataSource.isCallEnded(callId)) { return rejection('Call is not active', { callId, agent }); } @@ -216,10 +216,8 @@ export default class ProposalMutations { } // Check if there is an open call - const hasActiveCall = await this.callDataSource.checkActiveCall( - proposal.callId - ); - if (!isUserOfficer && !hasActiveCall) { + const isCallEnded = await this.callDataSource.isCallEnded(proposal.callId); + if (!isUserOfficer && isCallEnded) { return rejection('Can not submit proposal because call is not active', { agent, proposalPk, @@ -501,7 +499,7 @@ export default class ProposalMutations { } // Check if there is an open call - if (!(await this.callDataSource.checkActiveCall(callId))) { + if (await this.callDataSource.isCallEnded(callId)) { return rejection( 'Can not clone proposal because the call is not active', { callId, agent, sourceProposal } From e7a54194b12d33403c23e95f92cff8f03a88a7f7 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Thu, 23 Jun 2022 08:28:19 +0200 Subject: [PATCH 6/6] fix failing unit tests --- src/datasources/mockups/CallDataSource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datasources/mockups/CallDataSource.ts b/src/datasources/mockups/CallDataSource.ts index 02968132b..4199d6ced 100644 --- a/src/datasources/mockups/CallDataSource.ts +++ b/src/datasources/mockups/CallDataSource.ts @@ -157,6 +157,6 @@ export class CallDataSourceMock implements CallDataSource { } async isCallEnded(callId: number): Promise { - return callId === 1; + return callId !== 1; } }