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 6, 2023
2 parents af3463c + 0cc5ec4 commit 4b02d3e
Show file tree
Hide file tree
Showing 29 changed files with 686 additions and 199 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ jobs:
- name: Checkout
uses: actions/checkout@v1

- name: "Automated Version Bump"
id: version-bump
uses: "phips28/gh-action-bump-version@master"
with:
tag-prefix: "v"
env:
GITHUB_TOKEN: ${{ secrets.ADMIN_GITHUB_TOKEN }}

- name: Log in to Docker Hub
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
Expand Down
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ FROM node:16
# Create app directory
WORKDIR /usr/src/etenlab/crowd-rocks

COPY frontend /frontend/
COPY api /api/
COPY utils /utils/
COPY . /

ARG BUILD_MODE

Expand Down
2 changes: 2 additions & 0 deletions api/src/common/subscription-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class SubscriptionToken {
static pericopiesAdded = 'pericopiesAdded';
static pericopeDeleted = 'pericopeDeleted';
static pericopeVoteStatusToggled = 'pericopeVoteStatusToggled';
static bestPericopeTrChanged = 'bestPericopeTrChanged';
static recommendedPericopiesChanged = 'recommendedPericopiesChanged';
static questionsAdded = 'questionsAdded';
static questionsOnWordRangeAdded = 'questionsOnWordRangeAdded';
static answersAdded = 'answersAdded';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import { PericopiesModule } from '../pericopies/pericopies.module';
forwardRef(() => AuthenticationModule),
],
providers: [PericopeTrService, PericopeTrResolver],
exports: [],
exports: [PericopeTrService],
})
export class PericopeTrModule {}
68 changes: 50 additions & 18 deletions api/src/components/pericope-translations/pericope-tr.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger, UseGuards } from '@nestjs/common';
import { Inject, Injectable, Logger, UseGuards } from '@nestjs/common';
import {
Args,
Context,
Expand All @@ -7,25 +7,32 @@ import {
Mutation,
Query,
Resolver,
Subscription,
} from '@nestjs/graphql';
import { PericopeTrService } from './pericope-tr.service';
import {
AddPericopeTranslationInput,
BestPericopeTrChanged,
GetPericopeTranslationsInput,
GetPericopiesTrInput,
PericopeTranslation,
PericopeTranslationsOutput,
PericopeTrVoteStatusAndBestTrListOutput,
PericopeTrVoteStatusAndBestTrOutput,
PericopiesTextsWithTranslationConnection,
} from './types';
import { BearerTokenAuthGuard } from '../../guards/bearer-token-auth.guard';
import { ErrorType } from '../../common/types';
import { LanguageInput } from '../common/types';
import { PUB_SUB } from '../../pubSub.module';
import { PubSub } from 'graphql-subscriptions';
import { SubscriptionToken } from '../../common/subscription-token';

@Injectable()
@Resolver()
export class PericopeTrResolver {
constructor(private pericopeTrService: PericopeTrService) {}
constructor(
private pericopeTrService: PericopeTrService,
@Inject(PUB_SUB) private readonly pubSub: PubSub,
) {}

@Query(() => PericopiesTextsWithTranslationConnection)
async getPericopiesTr(
Expand Down Expand Up @@ -56,40 +63,57 @@ export class PericopeTrResolver {
}

@UseGuards(BearerTokenAuthGuard)
@Mutation(() => PericopeTrVoteStatusAndBestTrListOutput)
@Mutation(() => PericopeTrVoteStatusAndBestTrOutput)
async togglePericopeTrVoteStatus(
@Args('pericope_translation_id', { type: () => ID })
pericope_translation_id: string,
@Args('vote', { type: () => Boolean }) vote: boolean,
@Context() req: any,
): Promise<PericopeTrVoteStatusAndBestTrListOutput> {
): Promise<PericopeTrVoteStatusAndBestTrOutput> {
Logger.log(
`${JSON.stringify(pericope_translation_id)}`,
`PericopeTrResolver#togglePericopeTrVoteStatus`,
);

const vote_status_list = await this.pericopeTrService.toggleVoteStatus(
const { pericopeId, lang } = (
await this.pericopeTrService.getPericopeIdsAndLangsOfTranslationIds([
pericope_translation_id,
])
)[0];

const oldBestTr =
await this.pericopeTrService.getRecomendedPericopeTranslation(
pericopeId,
lang,
);

const vote_status = await this.pericopeTrService.toggleVoteStatus(
pericope_translation_id,
vote,
req.req.token as string,
);

const pIdsLangs: { pericopeId: string; lang: LanguageInput }[] =
await this.pericopeTrService.getPericopeIdsAndLangsOfTranslationIds(
vote_status_list.vote_status_list.map((v) => v.pericope_translation_id),
const best_translation =
await this.pericopeTrService.getRecomendedPericopeTranslation(
pericopeId,
lang,
);

const bestTrListPromises: Promise<PericopeTranslation | null>[] =
pIdsLangs.map((pIdLang) => {
return this.pericopeTrService.getRecomendedPericopeTranslation(
pIdLang.pericopeId,
pIdLang.lang,
);
if (
best_translation?.pericope_translation_id !==
oldBestTr?.pericope_translation_id
) {
this.pubSub.publish(SubscriptionToken.bestPericopeTrChanged, {
[SubscriptionToken.bestPericopeTrChanged]: {
newPericopeTr: best_translation,
newVoteStatus: vote_status.vote_status,
} as BestPericopeTrChanged,
});
}

return {
...vote_status_list,
best_translation_list: await Promise.all(bestTrListPromises),
...vote_status,
best_translation,
};
}

Expand Down Expand Up @@ -123,4 +147,12 @@ export class PericopeTrResolver {
};
}
}

@Subscription(() => BestPericopeTrChanged, {
name: SubscriptionToken.bestPericopeTrChanged,
})
async bestPericopeTrChanged() {
console.log('PericopeTrResolver#bestPericopeTrChanged');
return this.pubSub.asyncIterator(SubscriptionToken.bestPericopeTrChanged);
}
}
48 changes: 17 additions & 31 deletions api/src/components/pericope-translations/pericope-tr.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import {
GetPericopiesTrInput,
PericopeTranslation,
PericopeTranslationWithVotes,
PericopeTrVoteStatus,
PericopeTrVoteStatusListOutput,
PericopeTrVoteStatusOutput,
PericopiesTextsWithTranslationConnection,
PericopiesTextsWithTranslationEdge,
} from './types';
Expand Down Expand Up @@ -190,14 +189,13 @@ export class PericopeTrService {
currPericopeid = word.pericope_id;
currPericopeCursor++;
}
// it's better to make ordered_pericopies plain zero-based array
ordered_pericopies[currPericopeCursor - 1] = {
ordered_pericopies[currPericopeCursor] = {
pericopeCursor: currPericopeCursor,
pericopeId: currPericopeid,
words: [...(ordered_pericopies[currPericopeCursor]?.words || []), word],
};
});
return ordered_pericopies;
return ordered_pericopies.slice(1);
}

async getPericopeDescription(
Expand Down Expand Up @@ -343,7 +341,7 @@ export class PericopeTrService {
pericope_translation_id: string,
vote: boolean,
token: string,
): Promise<PericopeTrVoteStatusListOutput> {
): Promise<PericopeTrVoteStatusOutput> {
try {
const res = await this.pg.pool.query<TogglePericopeTrVoteStatusSqlR>(
...togglePericopeTrVoteStatusSql({
Expand All @@ -363,55 +361,43 @@ export class PericopeTrService {
) {
return {
error: creatingError,
vote_status_list: [],
vote_status: null,
};
}

return this.getVoteStatusFromIds([pericope_translation_id]);
return this.getVoteStatusOfPericopeTrId(pericope_translation_id);
} catch (e) {
console.error(e);
}

return {
error: ErrorType.UnknownError,
vote_status_list: [],
vote_status: null,
};
}

async getVoteStatusFromIds(
pericopeTrIds: string[],
): Promise<PericopeTrVoteStatusListOutput> {
async getVoteStatusOfPericopeTrId(
pericopeTrId: string,
): Promise<PericopeTrVoteStatusOutput> {
try {
const res = await this.pg.pool.query<GetPericopeTrVoteStatusSqlR>(
...getPericopeTrVoteStatusFromPericopeIdsSql(pericopeTrIds),
);

const voteStatusMap = new Map<string, PericopeTrVoteStatus>();

res.rows.forEach((row) =>
voteStatusMap.set(row.pericope_translation_id, row),
...getPericopeTrVoteStatusFromPericopeIdsSql([pericopeTrId]),
);

return {
error: ErrorType.NoError,
vote_status_list: pericopeTrIds.map((pericope_translation_id) => {
const voteStatus = voteStatusMap.get(pericope_translation_id + '');

return voteStatus
? voteStatus
: {
pericope_translation_id: pericope_translation_id + '',
upvotes: 0,
downvotes: 0,
};
}),
vote_status: {
pericope_translation_id: res.rows[0].pericope_translation_id,
upvotes: res.rows[0].upvotes || 0,
downvotes: res.rows[0].downvotes || 0,
},
};
} catch (e) {
Logger.error(e);
}
return {
error: ErrorType.UnknownError,
vote_status_list: [],
vote_status: null,
};
}

Expand Down
20 changes: 14 additions & 6 deletions api/src/components/pericope-translations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ export class PericopeTrVoteStatus {
}

@ObjectType()
export class PericopeTrVoteStatusListOutput extends GenericOutput {
@Field(() => [PericopeTrVoteStatus])
vote_status_list: PericopeTrVoteStatus[];
export class PericopeTrVoteStatusOutput extends GenericOutput {
@Field(() => PericopeTrVoteStatus)
vote_status: PericopeTrVoteStatus | null;
}
@ObjectType()
export class PericopeTrVoteStatusAndBestTrListOutput extends PericopeTrVoteStatusListOutput {
@Field(() => [PericopeTranslation])
best_translation_list: Array<PericopeTranslation | null>;
export class PericopeTrVoteStatusAndBestTrOutput extends PericopeTrVoteStatusOutput {
@Field(() => PericopeTranslation)
best_translation: PericopeTranslation | null;
}

@ObjectType()
export class BestPericopeTrChanged extends GenericOutput {
@Field(() => PericopeTranslation, { nullable: true })
newPericopeTr: PericopeTranslation | null;
@Field(() => PericopeTrVoteStatus, { nullable: true })
newVoteStatus: PericopeTrVoteStatus | null;
}
35 changes: 35 additions & 0 deletions api/src/components/pericopies/pericopies.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
PericopeWithVotesListConnection,
PericopeTextWithDescription,
PericopeDeleteOutput,
RecomendedPericopiesChangedAtDocumentId,
} from './types';

@Injectable()
Expand Down Expand Up @@ -103,9 +104,30 @@ export class PericopiesResolver {
[SubscriptionToken.pericopiesAdded]: newPericopies,
});

if (newPericopies.pericopies[0]?.pericope_id) {
const documentsData =
await this.pericopiesService.getDocumentIdsAndLangsOfPericopeIds([
newPericopies.pericopies[0]?.pericope_id,
]);
this.pubSub.publish(SubscriptionToken.recommendedPericopiesChanged, {
[SubscriptionToken.recommendedPericopiesChanged]: {
documentId: documentsData[0].documentId,
},
});
}

return newPericopies;
}

@Subscription(() => RecomendedPericopiesChangedAtDocumentId, {
name: SubscriptionToken.recommendedPericopiesChanged,
})
async subscribeToRecommendedPericopiesChanged() {
return this.pubSub.asyncIterator(
SubscriptionToken.recommendedPericopiesChanged,
);
}

@Subscription(() => PericopiesOutput, {
name: SubscriptionToken.pericopiesAdded,
})
Expand All @@ -121,6 +143,11 @@ export class PericopiesResolver {
): Promise<PericopeDeleteOutput> {
Logger.log('deletePericopies: ', pericope_id);

const documentsData =
await this.pericopiesService.getDocumentIdsAndLangsOfPericopeIds([
pericope_id,
]);

const deletedPericope = await this.pericopiesService.delete(
+pericope_id,
getBearer(req) || '',
Expand All @@ -131,6 +158,14 @@ export class PericopiesResolver {
[SubscriptionToken.pericopeDeleted]: deletedPericope,
});

if (documentsData[0].documentId) {
this.pubSub.publish(SubscriptionToken.recommendedPericopiesChanged, {
[SubscriptionToken.recommendedPericopiesChanged]: {
documentId: documentsData[0].documentId,
},
});
}

return deletedPericope;
}

Expand Down
Loading

0 comments on commit 4b02d3e

Please sign in to comment.