feat: allow multiple shares with one reverse share link

This commit is contained in:
Elias Schneider
2023-02-10 11:10:07 +01:00
parent edc10b72b7
commit ccdf8ea3ae
12 changed files with 171 additions and 75 deletions

View File

@@ -1,4 +1,4 @@
import { IsBoolean, IsString } from "class-validator";
import { IsBoolean, IsString, Max, Min } from "class-validator";
export class CreateReverseShareDTO {
@IsBoolean()
@@ -9,4 +9,8 @@ export class CreateReverseShareDTO {
@IsString()
shareExpiration: string;
@Min(1)
@Max(1000)
maxUseCount: number;
}

View File

@@ -3,7 +3,7 @@ import { Expose, plainToClass, Type } from "class-transformer";
import { MyShareDTO } from "src/share/dto/myShare.dto";
import { ReverseShareDTO } from "./reverseShare.dto";
export class ReverseShareTokenWithShare extends OmitType(ReverseShareDTO, [
export class ReverseShareTokenWithShares extends OmitType(ReverseShareDTO, [
"shareExpiration",
] as const) {
@Expose()
@@ -11,14 +11,17 @@ export class ReverseShareTokenWithShare extends OmitType(ReverseShareDTO, [
@Expose()
@Type(() => OmitType(MyShareDTO, ["recipients", "hasPassword"] as const))
share: Omit<
shares: Omit<
MyShareDTO,
"recipients" | "files" | "from" | "fromList" | "hasPassword"
>;
>[];
fromList(partial: Partial<ReverseShareTokenWithShare>[]) {
@Expose()
remainingUses: number;
fromList(partial: Partial<ReverseShareTokenWithShares>[]) {
return partial.map((part) =>
plainToClass(ReverseShareTokenWithShare, part, {
plainToClass(ReverseShareTokenWithShares, part, {
excludeExtraneousValues: true,
})
);

View File

@@ -15,7 +15,7 @@ import { JwtGuard } from "src/auth/guard/jwt.guard";
import { ConfigService } from "src/config/config.service";
import { CreateReverseShareDTO } from "./dto/createReverseShare.dto";
import { ReverseShareDTO } from "./dto/reverseShare.dto";
import { ReverseShareTokenWithShare } from "./dto/reverseShareTokenWithShare";
import { ReverseShareTokenWithShares } from "./dto/reverseShareTokenWithShares";
import { ReverseShareOwnerGuard } from "./guards/reverseShareOwner.guard";
import { ReverseShareService } from "./reverseShare.service";
@@ -51,7 +51,7 @@ export class ReverseShareController {
@Get()
@UseGuards(JwtGuard)
async getAllByUser(@GetUser() user: User) {
return new ReverseShareTokenWithShare().fromList(
return new ReverseShareTokenWithShares().fromList(
await this.reverseShareService.getAllByUser(user.id)
);
}

View File

@@ -34,6 +34,7 @@ export class ReverseShareService {
const reverseShare = await this.prisma.reverseShare.create({
data: {
shareExpiration: expirationDate,
remainingUses: data.maxUseCount,
maxShareSize: data.maxShareSize,
sendEmailNotification: data.sendEmailNotification,
creatorId,
@@ -60,7 +61,7 @@ export class ReverseShareService {
orderBy: {
shareExpiration: "desc",
},
include: { share: { include: { creator: true } } },
include: { shares: { include: { creator: true } } },
});
return reverseShares;
@@ -74,9 +75,9 @@ export class ReverseShareService {
if (!reverseShare) return false;
const isExpired = new Date() > reverseShare.shareExpiration;
const isUsed = reverseShare.used;
const remainingUsesExceeded = reverseShare.remainingUses <= 0;
return !(isExpired || isUsed);
return !(isExpired || remainingUsesExceeded);
}
async remove(id: string) {