Skip to content

Commit

Permalink
Pass transaction into find methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 18, 2024
1 parent 426ec05 commit 6d56be3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export async function createClass(options: CreateClassOptions): Promise<CreateCl
await ClassStories.create({
story_name: "hubbles_law",
class_id: cls.id
});
}, { transaction });
}

return creationInfo;
Expand Down Expand Up @@ -672,14 +672,18 @@ export async function newDummyClassForStory(storyName: string, transaction?: Tra
name: {
[Op.like]: `DummyClass_${storyName}_`
}
}
},
transaction: trans,
});
const cls = await Class.create({
educator_id: 0,
name: `DummyClass_${storyName}_${ct+1}`,
code: "xxxxxx"
}, { transaction: trans });
let dc = await DummyClass.findOne({ where: { story_name: storyName }} );
let dc = await DummyClass.findOne({
where: { story_name: storyName },
transaction: trans,
});
if (dc !== null) {
dc.update({ class_id: cls.id })
.catch(error => {
Expand Down Expand Up @@ -729,23 +733,32 @@ export async function newDummyStudent(seed = false,
// If we have a story name, and are creating a seed student, we want to add this student to the current "dummy class" for that story
if (seed && storyName !== null) {
let cls: Class | null = null;
let dummyClass = await DummyClass.findOne({ where: { story_name: storyName } });
let dummyClass = await DummyClass.findOne({
where: { story_name: storyName },
transaction
});
let clsSize: number;
if (dummyClass === null) {
const res = await newDummyClassForStory(storyName, transaction);
dummyClass = res.dummy;
cls = res.cls;
clsSize = 0;
} else {
clsSize = await StudentsClasses.count({ where: { class_id: dummyClass.class_id } });
clsSize = await StudentsClasses.count({
where: { class_id: dummyClass.class_id },
transaction,
});
}

const ct = Math.floor(Math.random() * 11) + 20;
if (clsSize > ct) {
const res = await newDummyClassForStory(storyName);
cls = res.cls;
} else {
cls = await Class.findOne({ where: { id: dummyClass.class_id } });
cls = await Class.findOne({
where: { id: dummyClass.class_id },
transaction,
});
}
if (cls !== null) {
await StudentsClasses.create({
Expand Down

0 comments on commit 6d56be3

Please sign in to comment.