-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Carifio24/eclipse-update-entries
Only store one response per user for eclipse mini
- Loading branch information
Showing
4 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
import { cosmicdsDB } from "../../database"; | ||
import { logger } from "../../logger"; | ||
import { | ||
isArrayThatSatisfies, | ||
isNumberArray, | ||
isStringArray | ||
} from "../../utils"; | ||
|
||
import { initializeModels, EclipseMiniResponse } from "./models"; | ||
|
||
initializeModels(cosmicdsDB); | ||
|
||
export async function submitEclipseMiniResponse(data: { | ||
user_uuid: string, | ||
response: string, | ||
export interface EclipseMiniData { | ||
user_uuid: string; | ||
response: string; | ||
preset_locations: string[], | ||
user_selected_locations: [number, number][], | ||
timestamp: Date | ||
}): Promise<EclipseMiniResponse | null> { | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function isValidEclipseMiniData(data: any): data is EclipseMiniData { | ||
|
||
return typeof data.user_uuid === "string" && | ||
typeof data.response === "string" && | ||
isStringArray(data.preset_locations) && | ||
isArrayThatSatisfies(data.user_selected_locations, (arr) => { | ||
return arr.every(x => isNumberArray(x) && x.length === 2); | ||
}); | ||
} | ||
|
||
export async function submitEclipseMiniResponse(data: EclipseMiniData): Promise<EclipseMiniResponse | null> { | ||
|
||
logger.verbose(`Attempting to submit measurement for user ${data.user_uuid}`); | ||
|
||
return EclipseMiniResponse.create({ | ||
const dataWithCounts = { | ||
...data, | ||
preset_locations_count: data.preset_locations.length, | ||
user_selected_locations_count: data.user_selected_locations.length | ||
}); | ||
}; | ||
|
||
return EclipseMiniResponse.upsert(dataWithCounts).then(([item, _]) => item); | ||
|
||
} | ||
|
||
export async function getAllEclipseMiniResponses(): Promise<EclipseMiniResponse[]> { | ||
return EclipseMiniResponse.findAll(); | ||
} | ||
|
||
export async function getEclipseMiniResponses(userUUID: string): Promise<EclipseMiniResponse[]> { | ||
return EclipseMiniResponse.findAll({ | ||
export async function getEclipseMiniResponse(userUUID: string): Promise<EclipseMiniResponse | null> { | ||
return EclipseMiniResponse.findOne({ | ||
where: { user_uuid: userUUID } | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters