Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/documents/document-upload.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Injectable, BadRequestException } from '@nestjs/common';

const ALLOWED_MIME_TYPES = new Set([
'application/pdf',
'image/jpeg',
'image/png',
'image/webp',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
]);

const MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB

export interface UploadRequest {
fileName: string;
mimeType: string;
fileSizeBytes: number;
}

export interface UploadMetadata {
fileName: string;
mimeType: string;
fileSizeBytes: number;
sanitisedName: string;
uploadedAt: string;
}

@Injectable()
export class DocumentUploadService {
validate(request: UploadRequest): void {
if (!ALLOWED_MIME_TYPES.has(request.mimeType)) {
throw new BadRequestException(`Unsupported file type: ${request.mimeType}`);
}
if (request.fileSizeBytes > MAX_FILE_SIZE_BYTES) {
throw new BadRequestException(
`File exceeds maximum allowed size of ${MAX_FILE_SIZE_BYTES / (1024 * 1024)} MB`,
);
}
if (!request.fileName.trim()) {
throw new BadRequestException('File name cannot be empty');
}
}

prepareMetadata(request: UploadRequest): UploadMetadata {
this.validate(request);
const sanitisedName = request.fileName
.trim()
.replace(/[^a-zA-Z0-9._-]/g, '_')
.toLowerCase();

return {
...request,
sanitisedName,
uploadedAt: new Date().toISOString(),
};
}
}
41 changes: 41 additions & 0 deletions src/documents/document-version.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';

export interface DocumentVersion {
versionNumber: number;
fileUrl: string;
updatedBy: string;
updatedAt: string;
changeNote?: string;
}

@Injectable()
export class DocumentVersionService {
private readonly store = new Map<string, DocumentVersion[]>();

addVersion(
documentId: string,
fileUrl: string,
updatedBy: string,
changeNote?: string,
): DocumentVersion {
const existing = this.store.get(documentId) ?? [];
const version: DocumentVersion = {
versionNumber: existing.length + 1,
fileUrl,
updatedBy,
updatedAt: new Date().toISOString(),
changeNote,
};
this.store.set(documentId, [...existing, version]);
return version;
}

getVersions(documentId: string): DocumentVersion[] {
return this.store.get(documentId) ?? [];
}

getLatest(documentId: string): DocumentVersion | null {
const versions = this.getVersions(documentId);
return versions.length > 0 ? (versions[versions.length - 1] ?? null) : null;
}
}
42 changes: 42 additions & 0 deletions src/search/image-search.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Injectable } from '@nestjs/common';

interface ImageSearchRequest {
imageUrl: string;
tags?: string[];
similarityThreshold?: number;
}

interface ImageSearchResult {
imageUrl: string;
tags: string[];
score: number;
}

@Injectable()
export class ImageSearchService {
private readonly index: ImageSearchResult[] = [];

index_image(entry: ImageSearchResult): void {
this.index.push(entry);
}

search(request: ImageSearchRequest): ImageSearchResult[] {
const threshold = request.similarityThreshold ?? 0.5;
const queryTags = request.tags ?? [];

if (queryTags.length === 0) return [...this.index];

return this.index
.map((item) => {
const matched = item.tags.filter((t) => queryTags.includes(t)).length;
const score = matched / queryTags.length;
return { ...item, score };
})
.filter((item) => item.score >= threshold)
.sort((a, b) => b.score - a.score);
}

clear(): void {
this.index.length = 0;
}
}
26 changes: 26 additions & 0 deletions src/search/voice-search.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';

interface VoiceSearchResult {
query: string;
normalised: string;
tokens: string[];
}

@Injectable()
export class VoiceSearchService {
private readonly FILLER_WORDS = new Set(['the', 'a', 'an', 'in', 'on', 'at', 'for', 'with']);

process(rawTranscript: string): VoiceSearchResult {
const normalised = rawTranscript.toLowerCase().replace(/[^a-z0-9\s]/g, '').trim();
const tokens = normalised
.split(/\s+/)
.filter((word) => word.length > 0 && !this.FILLER_WORDS.has(word));

return { query: rawTranscript, normalised, tokens };
}

buildSearchQuery(transcript: string): string {
const { tokens } = this.process(transcript);
return tokens.join(' ');
}
}
Loading