created basic endpoints for fetching single and for uploading
This commit is contained in:
parent
32b00a5417
commit
0ca35e85ef
|
|
@ -1,8 +1,9 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { PrismaModule } from './common/prisma/prisma.module';
|
import { PrismaModule } from './common/prisma/prisma.module';
|
||||||
|
import { CasesModule } from './cases/cases.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [PrismaModule],
|
imports: [PrismaModule, CasesModule],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
|
|
|
||||||
8
src/cases/cases.module.ts
Normal file
8
src/cases/cases.module.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { CasesService } from './cases.service';
|
||||||
|
import { CasesResolver } from './cases.resolver';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [CasesService, CasesResolver]
|
||||||
|
})
|
||||||
|
export class CasesModule {}
|
||||||
36
src/cases/cases.resolver.ts
Normal file
36
src/cases/cases.resolver.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { Resolver, Query, Mutation, Args, ID, Int, ResolveField, Parent } from '@nestjs/graphql';
|
||||||
|
import { GraphQLUpload } from 'graphql-upload-ts';
|
||||||
|
import type { FileUpload } from 'graphql-upload-ts';
|
||||||
|
import { CasesService } from './cases.service';
|
||||||
|
import { CaseLaw } from './entities/case-law.entity';
|
||||||
|
|
||||||
|
@Resolver(() => CaseLaw)
|
||||||
|
export class CasesResolver {
|
||||||
|
constructor(
|
||||||
|
private readonly casesService: CasesService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
|
||||||
|
@Query(() => CaseLaw, { name: 'caseLaw', nullable: true })
|
||||||
|
async findOne(
|
||||||
|
@Args('id', { type: () => String, nullable: true }) id?: string,
|
||||||
|
@Args('caseNumber', { type: () => String, nullable: true }) caseNumber?: string,
|
||||||
|
) {
|
||||||
|
return this.casesService.findOne(id, caseNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mutation(() => CaseLaw)
|
||||||
|
async uploadCase(
|
||||||
|
@Args({ name: 'file', type: () => GraphQLUpload })
|
||||||
|
{ createReadStream, filename, mimetype }: FileUpload,
|
||||||
|
) {
|
||||||
|
const chunks: any[] = [];
|
||||||
|
for await (const chunk of createReadStream()) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = Buffer.concat(chunks);
|
||||||
|
return this.casesService.processAndSave(buffer, mimetype, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
47
src/cases/cases.service.ts
Normal file
47
src/cases/cases.service.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { Injectable, NotFoundException, BadRequestException, Logger, Inject } from '@nestjs/common';
|
||||||
|
import { PRISMA_CLIENT, type PrismaClientInstance } from '../common/prisma/prisma.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CasesService {
|
||||||
|
private readonly logger = new Logger(CasesService.name);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
@Inject(PRISMA_CLIENT) private prisma: PrismaClientInstance,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async processAndSave(buffer: Buffer, mimetype: string, filename: string) {
|
||||||
|
this.logger.log(`Upload received: ${filename} (${mimetype}, ${(buffer.length / 1024).toFixed(1)} KB)`);
|
||||||
|
const fileType = mimetype === 'application/pdf' ? 'PDF' : 'HTML';
|
||||||
|
|
||||||
|
const caseLaw = await this.prisma.caseLaw.create({
|
||||||
|
data: {
|
||||||
|
title: `Processing: ${filename}`,
|
||||||
|
fileType,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return caseLaw;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async findOne(id?: string, caseNumber?: string) {
|
||||||
|
if (!id && !caseNumber) throw new BadRequestException('Provide ID or Case Number');
|
||||||
|
|
||||||
|
const caseLaw = await this.prisma.caseLaw.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: [
|
||||||
|
id && isUuid(id) ? { id } : undefined,
|
||||||
|
caseNumber ? { caseNumber } : undefined,
|
||||||
|
].filter(Boolean) as any,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!caseLaw) throw new NotFoundException('Case not found');
|
||||||
|
return caseLaw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isUuid = (value: string): boolean => {
|
||||||
|
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
||||||
|
};
|
||||||
|
|
||||||
37
src/cases/entities/case-law.entity.ts
Normal file
37
src/cases/entities/case-law.entity.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { ObjectType, Field, ID } from '@nestjs/graphql';
|
||||||
|
|
||||||
|
@ObjectType()
|
||||||
|
export class CaseLaw {
|
||||||
|
@Field(() => ID)
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
title: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
decisionType?: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
decisionDate?: Date;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
office?: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
court?: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
caseNumber?: string;
|
||||||
|
|
||||||
|
@Field({ nullable: true })
|
||||||
|
summary?: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
fileType: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue