-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release dev to master: recnet v1.14.2 / recnet-api v1.7.0 (#323)
## RecNet auto-release action This is an auto-generated PR by recnet-release-action 🤖 Please make sure to test your changes in staging before merging. ## Related Issues - #60 - #324 - #317 - #253 - #254 ## Related PRs - #322 - #325 - #320 - #311 ## Staging links recnet-web: [https://vercel.live/link/recnet-git-dev-recnet-542617e7.vercel.app](https://vercel.live/link/recnet-git-dev-recnet-542617e7.vercel.app) recnet-api: [https://dev-api.recnet.io/api](https://dev-api.recnet.io/api)
- Loading branch information
Showing
39 changed files
with
1,003 additions
and
356 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"name": "recnet-api", | ||
"version": "1.6.1" | ||
"version": "1.7.0" | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/recnet-api/prisma/migrations/20241007062222_add_rec_reaction_table/down.sql
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,12 @@ | ||
-- DropForeignKey | ||
ALTER TABLE "RecReaction" DROP CONSTRAINT "RecReaction_userId_fkey"; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE "RecReaction" DROP CONSTRAINT "RecReaction_recId_fkey"; | ||
|
||
-- DropTable | ||
DROP TABLE "RecReaction"; | ||
|
||
-- DropEnum | ||
DROP TYPE "ReactionType"; | ||
|
22 changes: 22 additions & 0 deletions
22
apps/recnet-api/prisma/migrations/20241007062222_add_rec_reaction_table/migration.sql
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,22 @@ | ||
-- CreateEnum | ||
CREATE TYPE "ReactionType" AS ENUM ('THUMBS_UP', 'THINKING', 'SURPRISED', 'CRYING', 'STARRY_EYES', 'MINDBLOWN', 'EYES', 'ROCKET', 'HEART', 'PRAY', 'PARTY'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "RecReaction" ( | ||
"id" SERIAL NOT NULL, | ||
"userId" VARCHAR(64) NOT NULL, | ||
"recId" VARCHAR(64) NOT NULL, | ||
"reaction" "ReactionType" NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "RecReaction_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "RecReaction_userId_recId_reaction_key" ON "RecReaction"("userId", "recId", "reaction"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "RecReaction" ADD CONSTRAINT "RecReaction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "RecReaction" ADD CONSTRAINT "RecReaction_recId_fkey" FOREIGN KEY ("recId") REFERENCES "Recommendation"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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
72 changes: 68 additions & 4 deletions
72
apps/recnet-api/src/database/repository/digital-library.repository.ts
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 |
---|---|---|
@@ -1,25 +1,89 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Prisma } from "@prisma/client"; | ||
|
||
import PrismaConnectionProvider from "@recnet-api/database/prisma/prisma.connection.provider"; | ||
|
||
import { digitalLibrary } from "./digital-library.repository.type"; | ||
import { | ||
CreateDigitalLibraryInput, | ||
DigitalLibrary, | ||
digitalLibrary, | ||
DigitalLibraryFilterBy, | ||
UpdateDigitalLibrariesRankInput, | ||
UpdateDigitalLibraryInput, | ||
} from "./digital-library.repository.type"; | ||
@Injectable() | ||
export default class DigitalLibraryRepository { | ||
constructor(private readonly prisma: PrismaConnectionProvider) {} | ||
|
||
public async findAll() { | ||
public async findAll(): Promise<Array<DigitalLibrary>> { | ||
return this.prisma.digitalLibrary.findMany({ | ||
orderBy: { | ||
rank: "asc", | ||
rank: Prisma.SortOrder.asc, | ||
}, | ||
select: digitalLibrary.select, | ||
}); | ||
} | ||
|
||
public async findById(id: number) { | ||
public async findById(id: number): Promise<DigitalLibrary> { | ||
return this.prisma.digitalLibrary.findUniqueOrThrow({ | ||
where: { id }, | ||
select: digitalLibrary.select, | ||
}); | ||
} | ||
|
||
public async findMany( | ||
filter: DigitalLibraryFilterBy | ||
): Promise<Array<DigitalLibrary>> { | ||
return this.prisma.digitalLibrary.findMany({ | ||
where: filter, | ||
select: digitalLibrary.select, | ||
}); | ||
} | ||
|
||
public async create( | ||
data: CreateDigitalLibraryInput | ||
): Promise<DigitalLibrary> { | ||
return this.prisma.digitalLibrary.create({ | ||
data, | ||
select: digitalLibrary.select, | ||
}); | ||
} | ||
|
||
public async update( | ||
id: number, | ||
data: UpdateDigitalLibraryInput | ||
): Promise<DigitalLibrary> { | ||
return this.prisma.digitalLibrary.update({ | ||
where: { id }, | ||
data, | ||
select: digitalLibrary.select, | ||
}); | ||
} | ||
|
||
public async delete(id: number): Promise<DigitalLibrary> { | ||
return this.prisma.digitalLibrary.delete({ | ||
where: { id }, | ||
}); | ||
} | ||
|
||
public async updateRanks( | ||
data: UpdateDigitalLibrariesRankInput | ||
): Promise<void> { | ||
// Update temp ranks to avoid rank conflicts | ||
const tempUpdates = data.map(({ id }, idx) => | ||
this.prisma.digitalLibrary.update({ | ||
where: { id }, | ||
data: { rank: -1 * (idx + 1) }, | ||
}) | ||
); | ||
|
||
const updates = data.map(({ id, rank }) => | ||
this.prisma.digitalLibrary.update({ | ||
where: { id }, | ||
data: { rank }, | ||
}) | ||
); | ||
|
||
await this.prisma.$transaction([...tempUpdates, ...updates]); | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
apps/recnet-api/src/modules/digital-library/digital-library.admin.service.ts
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,93 @@ | ||
import { HttpStatus, Inject, Injectable } from "@nestjs/common"; | ||
|
||
import DigitalLibraryRepository from "@recnet-api/database/repository/digital-library.repository"; | ||
import { | ||
DigitalLibrary as DbDigitalLibrary, | ||
DigitalLibraryFilterBy, | ||
} from "@recnet-api/database/repository/digital-library.repository.type"; | ||
import { RecnetError } from "@recnet-api/utils/error/recnet.error"; | ||
import { ErrorCode } from "@recnet-api/utils/error/recnet.error.const"; | ||
|
||
import { GetDigitalLibrariesResponse } from "./digital-library.response"; | ||
import { CreateDigitalLibraryDto } from "./dto/create.digital-library.dto"; | ||
import { | ||
UpdateDigitalLibrariesRankDto, | ||
UpdateDigitalLibraryDto, | ||
} from "./dto/update.digital-library.dto"; | ||
import { DigitalLibrary } from "./entities/digital-library.entity"; | ||
|
||
@Injectable() | ||
export class DigitalLibraryAdminService { | ||
constructor( | ||
@Inject(DigitalLibraryRepository) | ||
private readonly digitalLibraryRepository: DigitalLibraryRepository | ||
) {} | ||
|
||
public async getDigitalLibraries( | ||
filter: DigitalLibraryFilterBy | ||
): Promise<GetDigitalLibrariesResponse> { | ||
const digitalLibraries = | ||
await this.digitalLibraryRepository.findMany(filter); | ||
|
||
return { | ||
digitalLibraries: digitalLibraries.map(this.transformDigitalLibrary), | ||
}; | ||
} | ||
|
||
public async createDigitalLibrary( | ||
dto: CreateDigitalLibraryDto | ||
): Promise<DigitalLibrary> { | ||
const dbDigitalLibrary = await this.digitalLibraryRepository.create(dto); | ||
return this.transformDigitalLibrary(dbDigitalLibrary); | ||
} | ||
|
||
public async updateDigitalLibrary( | ||
id: number, | ||
dto: UpdateDigitalLibraryDto | ||
): Promise<DigitalLibrary> { | ||
const updatedDbDigitalLibrary = await this.digitalLibraryRepository.update( | ||
id, | ||
dto | ||
); | ||
return this.transformDigitalLibrary(updatedDbDigitalLibrary); | ||
} | ||
|
||
public async deleteDigitalLibrary(id: number): Promise<void> { | ||
await this.digitalLibraryRepository.delete(id); | ||
} | ||
|
||
public async updateDigitalLibrariesRank( | ||
dto: UpdateDigitalLibrariesRankDto | ||
): Promise<GetDigitalLibrariesResponse> { | ||
await this.validateRankUnique(dto); | ||
await this.digitalLibraryRepository.updateRanks(dto); | ||
const digitalLibraries = await this.digitalLibraryRepository.findAll(); | ||
return { | ||
digitalLibraries: digitalLibraries.map(this.transformDigitalLibrary), | ||
}; | ||
} | ||
|
||
private transformDigitalLibrary( | ||
digitalLibrary: DbDigitalLibrary | ||
): DigitalLibrary { | ||
return { | ||
id: digitalLibrary.id, | ||
name: digitalLibrary.name, | ||
regex: digitalLibrary.regex, | ||
rank: digitalLibrary.rank, | ||
isVerified: digitalLibrary.isVerified, | ||
}; | ||
} | ||
|
||
private async validateRankUnique( | ||
dto: UpdateDigitalLibrariesRankDto | ||
): Promise<void> { | ||
const ranks = dto.map((rank) => rank.rank); | ||
if (new Set(ranks).size !== dto.length) { | ||
throw new RecnetError( | ||
ErrorCode.DIGITAL_LIBRARY_RANK_CONFLICT, | ||
HttpStatus.CONFLICT | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.