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

add transactions on L&E webhook #489

Merged
merged 1 commit into from
Jan 10, 2023
Merged
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
160 changes: 102 additions & 58 deletions packages/core/src/services/learnAndEarn/syncRemote.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismicDocument } from '@prismicio/types';
import { Op } from 'sequelize';

import { models } from '../../database';
import { models, sequelize } from '../../database';
import { BaseError } from '../../utils/baseError';
import { client as prismic } from '../../utils/prismic';

Expand All @@ -11,48 +11,59 @@ import { client as prismic } from '../../utils/prismic';
// }

async function getPrismicLearnAndEarn() {
try {
const categoryIds: number[] = [],
levelIds: number[] = [],
lessonIds: number[] = [],
quizIds: number[] = [];
const categoryIds: number[] = [],
levelIds: number[] = [],
lessonIds: number[] = [],
quizIds: number[] = [];

const response = await prismic.getAllByType('pwa-lae-category', {
lang: 'en-us',
fetchLinks: [
'pwa-lae-level.reward',
'pwa-lae-level.active',
'pwa-lae-level.lessons',
'pwa-lae-lesson.questions',
'pwa-lae-lesson.active',
],
});
for (
let categoryIndex = 0;
categoryIndex < response.length;
categoryIndex++
) {
const t = await sequelize.transaction();

const response = await prismic.getAllByType('pwa-lae-category', {
lang: 'en-us',
fetchLinks: [
'pwa-lae-level.reward',
'pwa-lae-level.active',
'pwa-lae-level.lessons',
'pwa-lae-lesson.questions',
'pwa-lae-lesson.active',
],
});
for (
let categoryIndex = 0;
categoryIndex < response.length;
categoryIndex++
) {
try {
const prismicCategory = response[categoryIndex];
// INSERT CATEGORY
const [category] = await models.learnAndEarnCategory.findOrCreate({
where: {
prismicId: prismicCategory.id,
active: true,
},
transaction: t,
});

categoryIds.push(category.id);
await category.update({
languages: prismicCategory.alternate_languages
.map(({ lang }) => {
const index_ = lang.indexOf('-');
return lang.substring(
0,
index_ !== -1 ? index_ : lang.length
);
})
.concat(['en']),
});
await models.learnAndEarnCategory.update(
{
languages: prismicCategory.alternate_languages
.map(({ lang }) => {
const index_ = lang.indexOf('-');
return lang.substring(
0,
index_ !== -1 ? index_ : lang.length
);
})
.concat(['en']),
},
{
transaction: t,
where: {
id: category.id,
},
}
);

for (
let levelIndex = 0;
Expand All @@ -77,23 +88,32 @@ async function getPrismicLearnAndEarn() {
active: true,
categoryId: category.id,
},
transaction: t,
}
);
levelIds.push(level.id);
const findLevelLanguages = await prismic.getByID(
prismicLevel.id
);
await level.update({
languages: findLevelLanguages.alternate_languages
.map(({ lang }) => {
const index_ = lang.indexOf('-');
return lang.substring(
0,
index_ !== -1 ? index_ : lang.length
);
})
.concat(['en']),
});
await models.learnAndEarnLevel.update(
{
languages: findLevelLanguages.alternate_languages
.map(({ lang }) => {
const index_ = lang.indexOf('-');
return lang.substring(
0,
index_ !== -1 ? index_ : lang.length
);
})
.concat(['en']),
},
{
transaction: t,
where: {
id: level.id,
},
}
);

for (
let lessonIndex = 0;
Expand All @@ -119,25 +139,35 @@ async function getPrismicLearnAndEarn() {
isLive:
prismicLesson.data.active || false,
},
transaction: t,
});
lessonIds.push(lesson.id);
const findLessonLanguages = await prismic.getByID(
prismicLesson.id
);
await lesson.update({
languages:
findLessonLanguages.alternate_languages
.map(({ lang }) => {
const index_ = lang.indexOf('-');
return lang.substring(
0,
index_ !== -1
? index_
: lang.length
);
})
.concat(['en']),
});
await models.learnAndEarnLesson.update(
{
languages:
findLessonLanguages.alternate_languages
.map(({ lang }) => {
const index_ =
lang.indexOf('-');
return lang.substring(
0,
index_ !== -1
? index_
: lang.length
);
})
.concat(['en']),
},
{
transaction: t,
where: {
id: lesson.id,
},
}
);

for (
let quizIndex = 0;
Expand All @@ -160,16 +190,24 @@ async function getPrismicLearnAndEarn() {
lessonId: lesson.id,
answer,
},
transaction: t,
});
quizIds.push(quiz.id);
}
}
}
}
}
await t.commit();
} catch (error) {
await t.rollback();
console.log('e', error);
}
}

// INACTIVATE REGISTRIES
// INACTIVATE REGISTRIES
const t = await sequelize.transaction();
try {
await Promise.all([
models.learnAndEarnCategory.update(
{
Expand All @@ -181,6 +219,7 @@ async function getPrismicLearnAndEarn() {
[Op.notIn]: categoryIds,
},
},
transaction: t,
}
),
models.learnAndEarnLevel.update(
Expand All @@ -193,6 +232,7 @@ async function getPrismicLearnAndEarn() {
[Op.notIn]: levelIds,
},
},
transaction: t,
}
),
models.learnAndEarnLesson.update(
Expand All @@ -205,6 +245,7 @@ async function getPrismicLearnAndEarn() {
[Op.notIn]: lessonIds,
},
},
transaction: t,
}
),
models.learnAndEarnQuiz.update(
Expand All @@ -217,10 +258,13 @@ async function getPrismicLearnAndEarn() {
[Op.notIn]: quizIds,
},
},
transaction: t,
}
),
]);
await t.commit();
} catch (error) {
await t.rollback();
console.log('e', error);
}
}
Expand Down