2023-01-26 13:44:04 +01:00
|
|
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
|
|
import * as moment from "moment";
|
|
|
|
import { ConfigService } from "src/config/config.service";
|
|
|
|
import { FileService } from "src/file/file.service";
|
|
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
|
|
import { CreateReverseShareDTO } from "./dto/createReverseShare.dto";
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ReverseShareService {
|
|
|
|
constructor(
|
|
|
|
private config: ConfigService,
|
|
|
|
private prisma: PrismaService,
|
|
|
|
private fileService: FileService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async create(data: CreateReverseShareDTO, creatorId: string) {
|
|
|
|
// Parse date string to date
|
|
|
|
const expirationDate = moment()
|
|
|
|
.add(
|
|
|
|
data.shareExpiration.split("-")[0],
|
|
|
|
data.shareExpiration.split(
|
|
|
|
"-"
|
|
|
|
)[1] as moment.unitOfTime.DurationConstructor
|
|
|
|
)
|
|
|
|
.toDate();
|
|
|
|
|
|
|
|
const globalMaxShareSize = this.config.get("MAX_SHARE_SIZE");
|
|
|
|
|
|
|
|
if (globalMaxShareSize < data.maxShareSize)
|
|
|
|
throw new BadRequestException(
|
|
|
|
`Max share size can't be greater than ${globalMaxShareSize} bytes.`
|
|
|
|
);
|
|
|
|
|
|
|
|
const reverseShare = await this.prisma.reverseShare.create({
|
|
|
|
data: {
|
|
|
|
shareExpiration: expirationDate,
|
2023-02-10 11:10:07 +01:00
|
|
|
remainingUses: data.maxUseCount,
|
2023-01-26 13:44:04 +01:00
|
|
|
maxShareSize: data.maxShareSize,
|
|
|
|
sendEmailNotification: data.sendEmailNotification,
|
|
|
|
creatorId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return reverseShare.token;
|
|
|
|
}
|
|
|
|
|
2023-02-10 11:47:17 +01:00
|
|
|
async getByToken(reverseShareToken?: string) {
|
|
|
|
if (!reverseShareToken) return null;
|
|
|
|
|
2023-01-26 13:44:04 +01:00
|
|
|
const reverseShare = await this.prisma.reverseShare.findUnique({
|
|
|
|
where: { token: reverseShareToken },
|
|
|
|
});
|
|
|
|
|
|
|
|
return reverseShare;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllByUser(userId: string) {
|
|
|
|
const reverseShares = await this.prisma.reverseShare.findMany({
|
|
|
|
where: {
|
|
|
|
creatorId: userId,
|
|
|
|
shareExpiration: { gt: new Date() },
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
shareExpiration: "desc",
|
|
|
|
},
|
2023-02-10 11:10:07 +01:00
|
|
|
include: { shares: { include: { creator: true } } },
|
2023-01-26 13:44:04 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return reverseShares;
|
|
|
|
}
|
|
|
|
|
|
|
|
async isValid(reverseShareToken: string) {
|
|
|
|
const reverseShare = await this.prisma.reverseShare.findUnique({
|
|
|
|
where: { token: reverseShareToken },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!reverseShare) return false;
|
|
|
|
|
|
|
|
const isExpired = new Date() > reverseShare.shareExpiration;
|
2023-02-10 11:10:07 +01:00
|
|
|
const remainingUsesExceeded = reverseShare.remainingUses <= 0;
|
2023-01-26 13:44:04 +01:00
|
|
|
|
2023-02-10 11:10:07 +01:00
|
|
|
return !(isExpired || remainingUsesExceeded);
|
2023-01-26 13:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async remove(id: string) {
|
2023-02-10 11:15:23 +01:00
|
|
|
const shares = await this.prisma.share.findMany({
|
2023-01-26 13:44:04 +01:00
|
|
|
where: { reverseShare: { id } },
|
|
|
|
});
|
|
|
|
|
2023-02-10 11:15:23 +01:00
|
|
|
for (const share of shares) {
|
2023-01-26 13:44:04 +01:00
|
|
|
await this.prisma.share.delete({ where: { id: share.id } });
|
|
|
|
await this.fileService.deleteAllFiles(share.id);
|
|
|
|
}
|
2023-02-10 11:15:23 +01:00
|
|
|
|
|
|
|
await this.prisma.reverseShare.delete({ where: { id } });
|
2023-01-26 13:44:04 +01:00
|
|
|
}
|
|
|
|
}
|