-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bernardo Vieira <bernardo@impactmarket.com>
- Loading branch information
Joao Pedro da Silva
and
Bernardo Vieira
authored
May 16, 2023
1 parent
93c4d7c
commit 0596305
Showing
14 changed files
with
302 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/core/src/database/migrations/z1682361632-update-learnAndEarnUserLesson.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line no-undef | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
if (process.env.NODE_ENV === 'test') { | ||
return; | ||
} | ||
|
||
// add new columns | ||
await queryInterface.addColumn('learn_and_earn_user_lesson', 'levelId', { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'learn_and_earn_level', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
allowNull: true, | ||
}); | ||
|
||
await queryInterface.removeConstraint('learn_and_earn_user_lesson', 'learn_and_earn_user_lesson_lessonId_fkey'); | ||
|
||
// set old columns as optional | ||
await queryInterface.changeColumn('learn_and_earn_user_lesson', 'lessonId', { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}); | ||
}, | ||
down: (queryInterface) => {}, | ||
}; |
55 changes: 55 additions & 0 deletions
55
packages/core/src/database/migrations/z1682521792-update-learnAndEarnUserLesson.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const prismic = require('@prismicio/client'); | ||
|
||
const endpoint = prismic.getEndpoint(process.env.PRISMIC_REPO); | ||
const accessToken = process.env.PRISMIC_ACCESS_TOKEN; | ||
|
||
const client = prismic.createClient(endpoint, { accessToken }); | ||
|
||
// eslint-disable-next-line no-undef | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
if (process.env.NODE_ENV === 'test') { | ||
return; | ||
} | ||
|
||
// get all user lesson | ||
const userLessons = await queryInterface.sequelize.query( | ||
`select learn_and_earn_user_lesson.id as id, learn_and_earn_lesson."prismicId" as "prismicId", learn_and_earn_lesson."levelId" as "levelId" | ||
from learn_and_earn_user_lesson inner join learn_and_earn_lesson on learn_and_earn_user_lesson."lessonId" = learn_and_earn_lesson.id | ||
where learn_and_earn_user_lesson."levelId" is null`, | ||
{ type: Sequelize.QueryTypes.SELECT } | ||
); | ||
|
||
// get prismic id | ||
const prismicIds = userLessons.map((userLesson) => userLesson.prismicId); | ||
|
||
// get prismic document | ||
try { | ||
const prismicData = await client.getAllByIDs(prismicIds); | ||
|
||
userLessons.forEach(async (userLesson) => { | ||
const prismic = prismicData.find( | ||
(el) => el.id === userLesson.prismicId | ||
); | ||
const levelId = userLesson.levelId; | ||
const lessonId = prismic.data.id; | ||
|
||
if (!!levelId && !!lessonId) { | ||
await queryInterface.sequelize.query( | ||
`update learn_and_earn_user_lesson set "levelId" = ${levelId}, "lessonId" = ${lessonId} where id = ${userLesson.id}` | ||
); | ||
} | ||
}); | ||
|
||
// throw new Error(); | ||
|
||
} catch (error) { | ||
console.log(error); | ||
throw new Error(); | ||
|
||
} | ||
}, | ||
down: (queryInterface) => {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.