Skip to content

Commit

Permalink
Some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidJGapCR committed Oct 12, 2023
1 parent 4e3dc78 commit 8de9b4e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
import { DocumentUuid, MeadowlarkId } from '@edfi/meadowlark-core';

export interface ConcurrencyDocument {
_id: MeadowlarkId | DocumentUuid;
meadowlarkId: MeadowlarkId | null;
documentUuid: DocumentUuid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,7 +50,7 @@ export async function getNewClient(): Promise<MongoClient> {
const concurrencyCollection: Collection<ConcurrencyDocument> = 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) {
Expand Down Expand Up @@ -154,19 +154,35 @@ export const limitFive = (session: ClientSession): FindOptions => ({ limit: 5, s
* */
export async function insertMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection: Collection<ConcurrencyDocument>,
meadowlarkIds: ConcurrencyDocument[],
concurrencyDocuments: ConcurrencyDocument[],
): Promise<void> {
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);
}

export async function deleteMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection: Collection<ConcurrencyDocument>,
meadowlarkIds: ConcurrencyDocument[],
concurrencyDocuments: ConcurrencyDocument[],
): Promise<void> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export async function deleteDocumentByMeadowlarkIdTransaction(

const concurrencyDocuments: ConcurrencyDocument[] = [
{
_id: documentUuid,
meadowlarkId: null,
documentUuid,
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions Meadowlark-js/tests/http/RND-637.http
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
]
}

0 comments on commit 8de9b4e

Please sign in to comment.