Skip to content

Commit 54f5bd6

Browse files
committed
fix: add logic on insights repo on code level, remove from db
1 parent 4eddf57 commit 54f5bd6

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

backend/src/services/collectionService.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
queryCollections,
2121
queryInsightsProjectById,
2222
queryInsightsProjects,
23-
softDeleteInsightsProjectRepositories,
23+
softDeleteMissingInsightsProjectRepositories,
2424
updateCollection,
2525
updateInsightsProject,
2626
upsertInsightsProjectRepositories,
@@ -364,21 +364,20 @@ export class CollectionService extends LoggerBase {
364364
offset,
365365
}
366366
}
367-
368-
static normalizeRepositories(
369-
repositories?: string[] | { platform: string; url: string }[]
370-
): string[] {
371-
if (!repositories) return [];
372367

373-
if (typeof repositories[0] === 'string') {
374-
return repositories as string[];
375-
}
368+
static normalizeRepositories(
369+
repositories?: string[] | { platform: string; url: string }[],
370+
): string[] {
371+
if (!repositories) return []
376372

377-
return (repositories as { platform: string; url: string }[]).map(r => r.url);
378-
}
373+
if (typeof repositories[0] === 'string') {
374+
return repositories as string[]
375+
}
379376

377+
return (repositories as { platform: string; url: string }[]).map((r) => r.url)
378+
}
380379

381-
async updateInsightsProject(id: string, project: Partial<ICreateInsightsProject>) {
380+
async updateInsightsProject(insightsProjectId: string, project: Partial<ICreateInsightsProject>) {
382381
return SequelizeRepository.withTx(this.options, async (tx) => {
383382
const qx = SequelizeRepository.getQueryExecutor(this.options, tx)
384383

@@ -388,26 +387,23 @@ export class CollectionService extends LoggerBase {
388387
project.isLF = segment?.isLF ?? false
389388
}
390389

391-
await updateInsightsProject(qx, id, project)
392-
393-
console.log(`repositories: ${project.repositories} and id: ${id}`)
394-
395-
if (project.repositories?.length > 0) {
396-
// TODO: sistemare tutto in modo che le repository possono essere anche zero
397-
const repositories = CollectionService.normalizeRepositories(project.repositories)
398-
399-
await upsertInsightsProjectRepositories(qx, `${id}`, repositories)
400-
401-
await softDeleteInsightsProjectRepositories(qx, id, repositories)
402-
}
390+
await updateInsightsProject(qx, insightsProjectId, project)
391+
392+
const repositories = CollectionService.normalizeRepositories(project.repositories)
403393

394+
await upsertInsightsProjectRepositories(qx, { insightsProjectId, repositories })
395+
396+
await softDeleteMissingInsightsProjectRepositories(qx, {
397+
insightsProjectId,
398+
repositories,
399+
})
404400

405401
if (project.collections) {
406-
await disconnectProjectsAndCollections(qx, { insightsProjectId: id })
402+
await disconnectProjectsAndCollections(qx, { insightsProjectId })
407403
await connectProjectsAndCollections(
408404
qx,
409405
project.collections.map((c) => ({
410-
insightsProjectId: id,
406+
insightsProjectId,
411407
collectionId: c,
412408
starred: project.starred ?? true,
413409
})),
@@ -418,7 +414,7 @@ export class CollectionService extends LoggerBase {
418414
...this.options,
419415
transaction: tx,
420416
})
421-
return txSvc.findInsightsProjectById(id)
417+
return txSvc.findInsightsProjectById(insightsProjectId)
422418
})
423419
}
424420

services/libs/data-access-layer/src/collections/index.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,32 +295,53 @@ export async function findBySlug(qx: QueryExecutor, slug: string) {
295295
return collections
296296
}
297297

298-
export async function upsertInsightsProjectRepositories(qx: QueryExecutor, insightsProjectId, repositories: string[]) {
298+
export async function upsertInsightsProjectRepositories(
299+
qx: QueryExecutor,
300+
{
301+
insightsProjectId,
302+
repositories,
303+
}: {
304+
insightsProjectId: string
305+
repositories: string[]
306+
},
307+
): Promise<void> {
308+
if (repositories.length === 0) {
309+
return null
310+
}
311+
299312
const data = repositories.map((repo) => ({
300313
insightsProjectId,
301314
repository: repo,
302315
}))
303-
316+
304317
return qx.result(
305318
prepareBulkInsert(
306319
'insightsProjectsRepositories',
307320
['insightsProjectId', 'repository'],
308321
data,
309-
'do nothing',
322+
'("insightsProjectId", "repository") DO UPDATE SET "deletedAt" = NULL',
310323
),
311324
)
312325
}
313326

314-
export async function softDeleteInsightsProjectRepositories(
327+
export async function softDeleteMissingInsightsProjectRepositories(
315328
qx: QueryExecutor,
316-
insightsProjectId: string,
317-
currentRepositories: string[],
329+
{
330+
insightsProjectId,
331+
repositories,
332+
}: {
333+
insightsProjectId: string
334+
repositories: string[]
335+
},
318336
): Promise<void> {
319-
return qx.result(`
337+
return qx.result(
338+
`
320339
UPDATE "insightsProjectsRepositories"
321340
SET "deletedAt" = NOW()
322341
WHERE "insightsProjectId" = '${insightsProjectId}'
323342
AND "deletedAt" IS NULL
324-
AND "repository" NOT IN (${currentRepositories.map(repo => `'${repo}'`).join(', ')})
325-
`, { insightsProjectId, currentRepositories } )
343+
AND ${repositories.length > 0 ? `"repository" != ALL(ARRAY[${repositories.map((repo) => `'${repo}'`).join(', ')}])` : 'TRUE'}
344+
`,
345+
{ insightsProjectId, repositories },
346+
)
326347
}

0 commit comments

Comments
 (0)