Skip to content

Commit

Permalink
Merge pull request #492 from bettersg/develop
Browse files Browse the repository at this point in the history
3.3.6
  • Loading branch information
sarge1989 authored Oct 24, 2024
2 parents e624583 + 98de187 commit 6eb2daf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
6 changes: 4 additions & 2 deletions checkers-app/src/components/dashboard/CelebrationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface PropType {

export function CelebrationDialog({ display, certificateUrl }: PropType) {
const [open, setOpen] = React.useState(display);
const hasCompletedRef = React.useRef(false);
const { checkerDetails } = useUser();
const navigate = useNavigate();
const handleOpen = () => setOpen(!open);
Expand All @@ -32,13 +33,14 @@ export function CelebrationDialog({ display, certificateUrl }: PropType) {

React.useEffect(() => {
const completeCheckerProgram = async () => {
if (checkerDetails.checkerId) {
if (checkerDetails.checkerId && open && !hasCompletedRef.current) {
await completeProgram(checkerDetails.checkerId);
hasCompletedRef.current = true;
}
};

completeCheckerProgram();
}, []);
}, [open, checkerDetails.checkerId]);

return (
<Dialog open={open} handler={handleOpen}>
Expand Down
3 changes: 3 additions & 0 deletions functions/src/definitions/api/handlers/patchChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const patchCheckerHandler = async (req: Request, res: Response) => {
} else if (body.programData === "complete") {
//delete programdata from body
delete body.programData
if (checkerSnap.get("programData.programEnd") == null) {
return res.status(400).send("Program not yet able to be completed")
}
body["programData.isOnProgram"] = false
} else if (body.programData === "withdraw") {
const thresholds = await getThresholds()
Expand Down
29 changes: 20 additions & 9 deletions functions/src/definitions/common/responseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getThresholds, sleep } from "./utils"
import { getSignedUrl } from "./mediaUtils"
import { sendTextMessage } from "./sendMessage"
import { getVoteCounts } from "./counters"
import { CustomReply, UserBlast } from "../../types"
import { CustomReply, LanguageSelection, UserBlast } from "../../types"
import { incrementCheckerCounts } from "./counters"
import { get } from "http"

Expand Down Expand Up @@ -73,11 +73,11 @@ type ResponseObject = {
async function getResponsesObj(botType: "factChecker"): Promise<ResponseObject>
async function getResponsesObj(
botType: "user",
language: "en" | "cn"
language: LanguageSelection
): Promise<ResponseObject>
async function getResponsesObj(
botType: "user" | "factChecker" = "user",
language: "en" | "cn" = "en"
language: LanguageSelection = "en"
) {
let path
if (botType === "factChecker") {
Expand Down Expand Up @@ -178,11 +178,12 @@ async function sendMenuMessage(
disputedInstancePath: string | null = null,
isTruncated: boolean = false,
isGenerated: boolean = false,
isIncorrect: boolean = false
isIncorrect: boolean = false,
language: LanguageSelection | null = null
) {
const isSubscribedUpdates = userSnap.get("isSubscribedUpdates") ?? false
const language = userSnap.get("language") ?? "en"
const responses = await getResponsesObj("user", language)
const resolvedLanguage = language ?? userSnap.get("language") ?? "en"
const responses = await getResponsesObj("user", resolvedLanguage)
if (!(prefixName in responses)) {
functions.logger.error(`prefixName ${prefixName} not found in responses`)
return
Expand Down Expand Up @@ -375,7 +376,7 @@ async function getVotingStatsMessage(
truthScore: number | null,
numberPointScale: number,
messageRef: DocumentReference,
language: "en" | "cn" = "en"
language: LanguageSelection = "en"
) {
if (!messageRef) {
throw new Error("messageRef missing")
Expand Down Expand Up @@ -576,12 +577,22 @@ async function sendRationalisation(

async function updateLanguageAndSendMenu(
userSnap: DocumentSnapshot,
language: string
language: LanguageSelection
) {
await userSnap.ref.update({
language: language,
})
await sendMenuMessage(userSnap, "MENU_PREFIX", "whatsapp", null, null, true) //truncated menu on onboarding
await sendMenuMessage(
userSnap,
"MENU_PREFIX",
"whatsapp",
null,
null,
true,
false,
false,
language
) //truncated menu on onboarding
}

async function sendInterimUpdate(
Expand Down
2 changes: 0 additions & 2 deletions functions/src/definitions/eventHandlers/onCheckerUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ const onCheckerUpdateV2 = onDocumentUpdated(
// Existing code to get the checker app host URL
const url = `${checkerAppHost}/`

console.log(linkedInUrl)

// Fetch the base message template
const checkerResponses = await getResponsesObj("factChecker")
const baseMessage = checkerResponses.PROGRAM_COMPLETED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
respondToBlastFeedback,
} from "../common/responseUtils"
import { defineString } from "firebase-functions/params"
import { WhatsappMessageObject } from "../../types"
import { LanguageSelection, WhatsappMessageObject } from "../../types"
import { AppEnv } from "../../appEnv"

const runtimeEnvironment = defineString(AppEnv.ENVIRONMENT)
Expand Down Expand Up @@ -191,7 +191,7 @@ async function onButtonReply(
await respondToBlastFeedback(userSnap, blastPath, selection)
break
case "languageSelection":
;[selection] = rest
;[selection] = rest as [LanguageSelection]
await updateLanguageAndSendMenu(userSnap, selection)
break
}
Expand Down
4 changes: 3 additions & 1 deletion functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type WhatsappButton = {
}
}

export type LanguageSelection = "en" | "cn"

export type WhatsappMessageObject = {
from: string
type: string
Expand Down Expand Up @@ -226,7 +228,7 @@ export type UserData = {
referralCount: number
isReferralMessageSent: boolean
isReminderMessageSent: boolean //whether the response
language: "en" | "cn"
language: LanguageSelection
isSubscribedUpdates: boolean
isIgnored: boolean
}
Expand Down

0 comments on commit 6eb2daf

Please sign in to comment.