mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-04-03 15:26:16 +02:00
fix(Curated Collections): ensure resources are not duplicated on fetch-latest
This commit is contained in:
parent
d63c5bc668
commit
6b17e6ff68
|
|
@ -6,6 +6,14 @@ import type { BelongsTo } from '@adonisjs/lucid/types/relations'
|
||||||
export default class CuratedCollectionResource extends BaseModel {
|
export default class CuratedCollectionResource extends BaseModel {
|
||||||
static namingStrategy = new SnakeCaseNamingStrategy()
|
static namingStrategy = new SnakeCaseNamingStrategy()
|
||||||
|
|
||||||
|
static indexes = [
|
||||||
|
{
|
||||||
|
name: 'curated_collection_resources_unique',
|
||||||
|
columns: ['curated_collection_slug', 'url'],
|
||||||
|
unique: true,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
@column({ isPrimary: true })
|
@column({ isPrimary: true })
|
||||||
declare id: number
|
declare id: number
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -286,18 +286,26 @@ export class MapService implements IMapService {
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const collection of validated.collections) {
|
for (const collection of validated.collections) {
|
||||||
const collectionResult = await CuratedCollection.updateOrCreate(
|
const { resources, ...restCollection } = collection; // we'll handle resources separately
|
||||||
{ slug: collection.slug },
|
|
||||||
|
// Upsert the collection itself
|
||||||
|
await CuratedCollection.updateOrCreate(
|
||||||
|
{ slug: restCollection.slug },
|
||||||
{
|
{
|
||||||
...collection,
|
...restCollection,
|
||||||
type: 'map',
|
type: 'map',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
logger.info(`[MapService] Upserted curated collection: ${collection.slug}`)
|
logger.info(`[MapService] Upserted curated collection: ${restCollection.slug}`)
|
||||||
|
|
||||||
|
// Upsert collection's resources
|
||||||
|
const resourcesResult = await CuratedCollectionResource.updateOrCreateMany('url', resources.map((res) => ({
|
||||||
|
...res,
|
||||||
|
curated_collection_slug: restCollection.slug, // add the foreign key
|
||||||
|
})))
|
||||||
|
|
||||||
await collectionResult.related('resources').createMany(collection.resources)
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`[MapService] Upserted ${collection.resources.length} resources for collection: ${collection.slug}`
|
`[MapService] Upserted ${resourcesResult.length} resources for collection: ${restCollection.slug}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { BaseSchema } from '@adonisjs/lucid/schema'
|
||||||
|
|
||||||
|
export default class extends BaseSchema {
|
||||||
|
protected tableName = 'curated_collection_resources'
|
||||||
|
|
||||||
|
async up() {
|
||||||
|
this.schema.alterTable(this.tableName, (table) => {
|
||||||
|
table.unique(['curated_collection_slug', 'url'], {
|
||||||
|
indexName: 'curated_collection_resources_unique',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async down() {
|
||||||
|
this.schema.alterTable(this.tableName, (table) => {
|
||||||
|
table.dropUnique(['curated_collection_slug', 'url'], 'curated_collection_resources_unique')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user