Skip to content

Commit

Permalink
Merge pull request #455 from claushaas/dev
Browse files Browse the repository at this point in the history
update migration service
  • Loading branch information
claushaas authored Jan 14, 2025
2 parents be303aa + 62e688f commit 88dd0c4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 111 deletions.
3 changes: 0 additions & 3 deletions app/routes/admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ export default function Admin() {
<Link to='/admin/repopulate-cache'>
<p>Repopular cache</p>
</Link>
<Link to='/admin/migrate-lesson-activity'>
<p>Migrar atividade de aulas</p>
</Link>
</aside>
<main className='flex-grow flex-shrink p-3'>
{(pathname === '/admin' || pathname === '/admin/') && (
Expand Down
36 changes: 17 additions & 19 deletions app/services/migration.service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class MigrationService {

public async migrateSavedAndFavoritedLessonsForUsers(users: UserType[]): Promise<TServiceReturn<string>> {
try {
process.setMaxListeners(0);

users.forEach(async user => { // eslint-disable-line unicorn/no-array-for-each
const userId = user.Attributes?.find(attribute => attribute.Name === 'custom:id')?.Value ?? user.Attributes!.find(attribute => attribute.Name === 'sub')!.Value!;
const newUserId = user.Attributes!.find(attribute => attribute.Name === 'sub')!.Value!;
Expand Down Expand Up @@ -69,19 +71,17 @@ export class MigrationService {
};
});

// eslint-disable-next-line unicorn/no-array-for-each
favoritedLessonsArray.filter(element => element !== null).forEach(async element => {
await this._model.favoritedLessons.upsert({
const array = favoritedLessonsArray.filter(element => element !== null);

await Promise.all(array.map(async element =>
this._model.favoritedLessons.upsert({
create: element,
update: element,
where: {
userId_lessonSlug: { // eslint-disable-line @typescript-eslint/naming-convention
lessonSlug: element.lessonSlug,
userId: element.userId,
},
userId_lessonSlug: element, // eslint-disable-line @typescript-eslint/naming-convention
},
});
});
}),
));
}

if (completedLessons && completedLessons.length > 0) {
Expand All @@ -104,19 +104,17 @@ export class MigrationService {
};
});

// eslint-disable-next-line unicorn/no-array-for-each
completedLessonsArray.filter(element => element !== null).forEach(async element => {
const array = completedLessonsArray.filter(element => element !== null);

await Promise.all(array.map(async element => {
await this._model.completedLessons.upsert({
create: element,
update: element,
where: {
lessonSlug_userId: { // eslint-disable-line @typescript-eslint/naming-convention
lessonSlug: element.lessonSlug,
userId: element.userId,
},
lessonSlug_userId: element, // eslint-disable-line @typescript-eslint/naming-convention
},
});
});
}));
}
});

Expand All @@ -141,16 +139,16 @@ export class MigrationService {
PaginationToken: paginationToken ?? undefined,
});

const addUsersToList = async (paginationToken: string | undefined, pages = 0) => {
const addUsersToList = async (paginationToken: string | undefined) => {
try {
const result = await this._awsClient.send(listUsersCommand(paginationToken));

if (result.Users) {
await this.migrateSavedAndFavoritedLessonsForUsers(result.Users);
}

if (result.PaginationToken && pages < 150) {
await addUsersToList(result.PaginationToken, pages + 1);
if (result.PaginationToken) {
await addUsersToList(result.PaginationToken);
}
} catch (error) {
logger.logError((error as Error).message);
Expand Down
Loading

0 comments on commit 88dd0c4

Please sign in to comment.