diff --git a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/model/ConcurrencyDocument.ts b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/model/ConcurrencyDocument.ts index 81e1485b..af92667d 100644 --- a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/model/ConcurrencyDocument.ts +++ b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/model/ConcurrencyDocument.ts @@ -6,5 +6,6 @@ import { DocumentUuid, MeadowlarkId } from '@edfi/meadowlark-core'; export interface ConcurrencyDocument { - _id: MeadowlarkId | DocumentUuid; + meadowlarkId: MeadowlarkId | null; + documentUuid: DocumentUuid; } diff --git a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Db.ts b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Db.ts index 989cc859..497c87aa 100644 --- a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Db.ts +++ b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Db.ts @@ -14,7 +14,7 @@ export const DOCUMENT_COLLECTION_NAME = 'documents'; export const AUTHORIZATION_COLLECTION_NAME = 'authorizations'; // RND-644 -export const CONCURRENCY_COLLECTION_NAME = 'concurrences'; +export const CONCURRENCY_COLLECTION_NAME = 'concurrency'; let singletonClient: MongoClient | null = null; @@ -50,7 +50,7 @@ export async function getNewClient(): Promise { const concurrencyCollection: Collection = newClient .db(databaseName) .collection(CONCURRENCY_COLLECTION_NAME); - await concurrencyCollection.createIndex({ meadowlarkId: 1 }); + await concurrencyCollection.createIndex({ meadowlarkId: 1, documentUuid: 1 }, { unique: true }); // RND-644 return newClient; } catch (e) { @@ -154,9 +154,9 @@ export const limitFive = (session: ClientSession): FindOptions => ({ limit: 5, s * */ export async function insertMeadowlarkIdOnConcurrencyCollection( concurrencyCollection: Collection, - meadowlarkIds: ConcurrencyDocument[], + concurrencyDocuments: ConcurrencyDocument[], ): Promise { - const { acknowledged, insertedCount, insertedIds } = await concurrencyCollection.insertMany(meadowlarkIds); + const { acknowledged, insertedCount, insertedIds } = await concurrencyCollection.insertMany(concurrencyDocuments); // eslint-disable-next-line no-console console.log(acknowledged, insertedCount, insertedIds); @@ -164,9 +164,25 @@ export async function insertMeadowlarkIdOnConcurrencyCollection( export async function deleteMeadowlarkIdOnConcurrencyCollection( concurrencyCollection: Collection, - meadowlarkIds: ConcurrencyDocument[], + concurrencyDocuments: ConcurrencyDocument[], ): Promise { - const { acknowledged, deletedCount } = await concurrencyCollection.deleteMany({ meadowlarkIds }); + const meadowlarkIds: any[] = concurrencyDocuments.map((document) => document.meadowlarkId); + const documentUuids: any[] = concurrencyDocuments.map((document) => document.documentUuid); + + const { acknowledged, deletedCount } = await concurrencyCollection.deleteMany({ + $and: [ + { + meadowlarkId: { + $in: meadowlarkIds, + }, + }, + { + documentUuid: { + $in: documentUuids, + }, + }, + ], + }); // eslint-disable-next-line no-console console.log(acknowledged, deletedCount); diff --git a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Delete.ts b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Delete.ts index 5f9ef26e..af31a493 100644 --- a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Delete.ts +++ b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Delete.ts @@ -104,7 +104,8 @@ export async function deleteDocumentByMeadowlarkIdTransaction( const concurrencyDocuments: ConcurrencyDocument[] = [ { - _id: documentUuid, + meadowlarkId: null, + documentUuid, }, ]; diff --git a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Update.ts b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Update.ts index 04b86f9b..ea35acf4 100644 --- a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Update.ts +++ b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Update.ts @@ -171,10 +171,11 @@ async function updateAllowingIdentityChange( // Ensure referenced documents are not modified in other transactions await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session); - const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({ - _id: ref, + const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((reference) => ({ + meadowlarkId: reference, + documentUuid: updateRequest.documentUuid, })); - concurrencyDocuments.push({ _id: updateRequest.meadowlarkId }); + concurrencyDocuments.push({ meadowlarkId: updateRequest.meadowlarkId, documentUuid: updateRequest.documentUuid }); await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644 @@ -260,10 +261,11 @@ async function updateDisallowingIdentityChange( // Ensure referenced documents are not modified in other transactions await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session); - const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({ - _id: ref, + const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((reference) => ({ + meadowlarkId: reference, + documentUuid: updateRequest.documentUuid, })); - concurrencyDocuments.push({ _id: updateRequest.meadowlarkId }); + concurrencyDocuments.push({ meadowlarkId: updateRequest.meadowlarkId, documentUuid: updateRequest.documentUuid }); await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644 diff --git a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Upsert.ts b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Upsert.ts index 76652cdd..dde6205e 100644 --- a/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Upsert.ts +++ b/Meadowlark-js/backends/meadowlark-mongodb-backend/src/repository/Upsert.ts @@ -142,10 +142,11 @@ export async function upsertDocumentTransaction( await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session); - const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({ - _id: ref, + const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((reference) => ({ + meadowlarkId: reference, + documentUuid, })); - concurrencyDocuments.push({ _id: meadowlarkId }); + concurrencyDocuments.push({ meadowlarkId, documentUuid }); await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644 // Perform the document upsert diff --git a/Meadowlark-js/tests/http/RND-637.http b/Meadowlark-js/tests/http/RND-637.http index 943d9ab8..1d513948 100644 --- a/Meadowlark-js/tests/http/RND-637.http +++ b/Meadowlark-js/tests/http/RND-637.http @@ -64,13 +64,14 @@ authorization: bearer {{authToken1}} ### This returns 409 error, since uri://ed-fi.org/SchoolCategoryDescriptor#All Levels # and uri://ed-fi.org/GradeLevelDescriptor#First Grade don't exist. +# @name theSchool POST http://localhost:3000/local/v3.3b/ed-fi/schools content-type: application/json authorization: bearer {{authToken1}} { - "schoolId": 124, - "nameOfInstitution": "A School", + "schoolId": 125, + "nameOfInstitution": "A School 127", "educationOrganizationCategories" : [ { "educationOrganizationCategoryDescriptor": "uri://ed-fi.org/EducationOrganizationCategoryDescriptor#Other" @@ -115,3 +116,30 @@ authorization: bearer {{authToken1}} ##### GET http://localhost:3000/local/v3.3b/ed-fi/schools?nameOfInstitution=A School authorization: bearer {{authToken1}} + +### This returns 409 error, since uri://ed-fi.org/SchoolCategoryDescriptor#All Levels +# and uri://ed-fi.org/GradeLevelDescriptor#First Grade don't exist. +PUT http://localhost:3000{{theSchool.response.headers.location}} +content-type: application/json +authorization: bearer {{authToken1}} + +{ + "id": "28daa358-6154-4973-aad3-2f4073c6544f", + "schoolId": 125, + "nameOfInstitution": "A School 127", + "educationOrganizationCategories" : [ + { + "educationOrganizationCategoryDescriptor": "uri://ed-fi.org/EducationOrganizationCategoryDescriptor#Other" + } + ], + "schoolCategories": [ + { + "schoolCategoryDescriptor": "uri://ed-fi.org/SchoolCategoryDescriptor#All Levels" + } + ], + "gradeLevels": [ + { + "gradeLevelDescriptor": "uri://ed-fi.org/GradeLevelDescriptor#First Grade" + } + ] +}