Skip to content

Commit

Permalink
Merge branch 'dev' into Documents_Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Piyush Patel committed Dec 8, 2023
2 parents 4b02d3e + d444453 commit 8bcc753
Show file tree
Hide file tree
Showing 36 changed files with 1,716 additions and 41 deletions.
4 changes: 4 additions & 0 deletions api/src/common/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function calc_vote_weight(upvotes: number, downvotes: number): number {
export const putLangCodesToFileName = (
file_name: string,
langCodes: LanguageInput,
customSuffix?: string,
): string => {
if (!langCodes.language_code) {
throw new Error(`language_code insn't provided!`);
Expand All @@ -76,6 +77,9 @@ export const putLangCodesToFileName = (
if (langCodes.geo_code) {
fname += `-${langCodes.geo_code}`;
}
if (customSuffix?.length && customSuffix?.length > 0) {
fname += `.${customSuffix}`;
}
fname += '.' + suffixes.join('.');
return fname;
};
Expand Down
94 changes: 94 additions & 0 deletions api/src/components/documents/document-translation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Injectable } from '@nestjs/common';
import { LanguageInput } from '../common/types';
import { PericopeTrService } from '../pericope-translations/pericope-tr.service';
import {
PericopiesService,
WORDS_JOINER,
} from '../pericopies/pericopies.service';
import { FileService } from '../file/file.service';
import { FileUrlOutput } from './types';
import { Readable } from 'stream';
import { DocumentsService } from './documents.service';
import { putLangCodesToFileName } from '../../common/utility';
import { ErrorType } from '../../common/types';

const DEFAULT_TEXTY_FILE_MIME_TYPE = 'text/plain';
const TEXT_IF_NO_TRANSLATION_NO_ORIGINAL = '_error_';

@Injectable()
export class DocumentTranslateService {
constructor(
private readonly documentsService: DocumentsService,
private readonly pericopeTrService: PericopeTrService,
private readonly pericopiesService: PericopiesService,
private readonly fileService: FileService,
) {}

async translateByPericopies(
documentId: string,
targetLang: LanguageInput,
): Promise<FileUrlOutput> {
const document = await this.documentsService.getDocument(
Number(documentId),
);
if (document.error !== ErrorType.NoError || !document.document?.file_name) {
return {
error: ErrorType.DocumentNotFound,
fileUrl: null,
fileName: null,
};
}
const start_word = await this.pericopiesService.getFirstWordOfDocument(
documentId,
);
const allWords =
start_word === null
? []
: await this.pericopiesService.getWordsTillEndOfDocument(
documentId,
start_word,
);
const pericopeIds: string[] = [];
allWords.forEach((word) => {
if (word.pericope_id) {
pericopeIds.push(word.pericope_id);
}
});
const pericopeTranslationsPromises = pericopeIds.map((pId) =>
this.pericopeTrService.getRecomendedPericopeTranslation(pId, targetLang),
);
const pericopiesOriginalsPromises = pericopeIds.map((pid) =>
this.pericopiesService.getPericopeTextWithDescription(pid),
);

const pericopiesTranslations = await Promise.all(
pericopeTranslationsPromises,
);
const pericopiesOriginals = await Promise.all(pericopiesOriginalsPromises);
const finalStrings = pericopeIds.map((pId) => {
const tr = pericopiesTranslations.find(
(pt) => pt?.pericope_id === pId,
)?.translation;
return (
tr ||
pericopiesOriginals.find((po) => po.pericope_id === pId)
?.pericope_text ||
TEXT_IF_NO_TRANSLATION_NO_ORIGINAL
);
});

const fileContentStream = Readable.from([finalStrings.join(WORDS_JOINER)]);
const newFileName = putLangCodesToFileName(
document.document.file_name,
targetLang,
'by_pericopies',
);

const fileUrl = await this.fileService.uploadTemporaryFile(
fileContentStream,
newFileName,
DEFAULT_TEXTY_FILE_MIME_TYPE,
);
return { error: ErrorType.NoError, fileUrl, fileName: newFileName };
}
}
7 changes: 7 additions & 0 deletions api/src/components/documents/documents.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import { DocumentWordEntriesService } from './document-word-entries.service';
import { WordRangesService } from './word-ranges.service';
import { AuthorizationModule } from '../authorization/authorization.module';
import { AuthorizationService } from '../authorization/authorization.service';
import { DocumentTranslateService } from './document-translation.service';
import { PericopeTrModule } from '../pericope-translations/pericope-tr.module';
import { PericopiesModule } from '../pericopies/pericopies.module';

@Module({
imports: [
forwardRef(() => CoreModule),
forwardRef(() => AuthenticationModule),
forwardRef(() => WordsModule),
forwardRef(() => AuthorizationModule),
forwardRef(() => PericopeTrModule),
forwardRef(() => PericopiesModule),
forwardRef(() => DocumentsModule),
FileModule,
],
providers: [
Expand All @@ -26,6 +32,7 @@ import { AuthorizationService } from '../authorization/authorization.service';
WordRangesService,
DocumentsResolver,
AuthorizationService,
DocumentTranslateService,
],
exports: [DocumentsService, DocumentWordEntriesService, WordRangesService],
})
Expand Down
24 changes: 23 additions & 1 deletion api/src/components/documents/documents.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import {
WordRangesOutput,
WordRangeInput,
WordRangesListConnection,
TranslateDocumentByPericopiesInput,
FileUrlOutput,
} from './types';
import { LanguageInput } from '../common/types';
import { DocumentTranslateService } from './document-translation.service';

@Injectable()
@Resolver()
Expand All @@ -44,6 +47,7 @@ export class DocumentsResolver {
private documentsSevice: DocumentsService,
private documentWordEntriesService: DocumentWordEntriesService,
private wordRangesService: WordRangesService,
private documentTranslateService: DocumentTranslateService,
) {}

@Mutation(() => DocumentUploadOutput)
Expand Down Expand Up @@ -186,11 +190,29 @@ export class DocumentsResolver {
input: WordRangeInput[],
@Context() req: any,
): Promise<WordRangesOutput> {
Logger.log('upsertWordRanges: ', JSON.stringify(input, null, 2));
Logger.log(
'DocumentsResolver#upsertWordRanges: ',
JSON.stringify(input, null, 2),
);

return this.wordRangesService.upserts(input, getBearer(req) || '', null);
}

@Mutation(() => FileUrlOutput)
async documentByPericopiesTranslate(
@Args('input', { type: () => TranslateDocumentByPericopiesInput })
input: TranslateDocumentByPericopiesInput,
): Promise<FileUrlOutput> {
Logger.log(
'DocumentsResolver#documentByPericopiesTranslate: ',
JSON.stringify(input, null, 2),
);
return this.documentTranslateService.translateByPericopies(
input.documentId,
input.targetLang,
);
}

@Subscription(() => DocumentUploadOutput, {
name: SubscriptionToken.documentAdded,
})
Expand Down
14 changes: 13 additions & 1 deletion api/src/components/documents/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, ID, Int, InputType, ObjectType } from '@nestjs/graphql';
import { GenericOutput } from '../../common/types';
import { PageInfo } from '../common/types';
import { LanguageInput, PageInfo } from '../common/types';

import { WordlikeString } from '../words/types';

Expand Down Expand Up @@ -131,3 +131,15 @@ export class TextFromRange {
export class TextFromRangesOutput extends GenericOutput {
@Field(() => [TextFromRange]) list: TextFromRange[];
}

@InputType()
export class TranslateDocumentByPericopiesInput {
@Field(() => String) documentId: string;
@Field(() => LanguageInput) targetLang: LanguageInput;
}

@ObjectType()
export class FileUrlOutput extends GenericOutput {
@Field(() => String, { nullable: true }) fileUrl: string | null;
@Field(() => String, { nullable: true }) fileName: string | null;
}
3 changes: 3 additions & 0 deletions api/src/components/pericopies/pericopies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ export class PericopiesService {
);
}

/**
* @returns WordsTillEndOfDocumentSqlR[] - words are already ordered by level of joining, i.e. by the order of appearence in the text
*/
async getWordsTillEndOfDocument(
documentId: string,
start_word_id: string,
Expand Down
12 changes: 12 additions & 0 deletions api/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ type FileUploadUrlResponse {
url: String!
}

type FileUrlOutput {
error: ErrorType!
fileName: String
fileUrl: String
}

type Flag {
created_at: String!
created_by: ID!
Expand Down Expand Up @@ -675,6 +681,7 @@ type Mutation {
createQuestionOnWordRange(input: CreateQuestionOnWordRangeUpsertInput!): QuestionOnWordRangesOutput!
createTaggingOnWordRange(begin_document_word_entry_id: ID!, end_document_word_entry_id: ID!, tag_names: [String!]!): WordRangeTagWithVotesOutput!
deletePericopie(pericope_id: ID!): PericopeDeleteOutput!
documentByPericopiesTranslate(input: TranslateDocumentByPericopiesInput!): FileUrlOutput!
documentUpload(input: DocumentUploadInput!): DocumentUploadOutput!
emailResponseResolver(input: EmailResponseInput!): EmailResponseOutput!
forceMarkAndRetranslateOriginalMapsIds(originalMapsIds: [String!]!): GenericOutput!
Expand Down Expand Up @@ -1667,6 +1674,11 @@ type TranslateAllWordsAndPhrasesByBotResult {
translatedWordCount: Int!
}

input TranslateDocumentByPericopiesInput {
documentId: String!
targetLang: LanguageInput!
}

input TranslatedLanguageInfoInput {
fromLanguageCode: ID!
toLanguageCode: ID
Expand Down
1 change: 0 additions & 1 deletion frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ dist/
/test-results/
/playwright-report/
/playwright/.cache/
/public/meta.json
Loading

0 comments on commit 8bcc753

Please sign in to comment.