Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Auto-fill country, timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 3, 2020
1 parent 8de9432 commit e9baebd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .staartrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"controllerPrefix": "v1",
"genderPrediction": true
"registerGenderPrediction": true,
"registerLocationDetection": true
}
2 changes: 1 addition & 1 deletion src/_staart/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const register = async (
groupId = domainDetails.groupId;
} catch (error) {}
}
const userId = (await createUser(user)).id;
const userId = (await createUser(user, locals)).id;
if (groupId) {
await prisma.memberships.create({
data: {
Expand Down
18 changes: 15 additions & 3 deletions src/_staart/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ import { mail } from "../helpers/mail";
import { prisma } from "../helpers/prisma";
import { deleteSensitiveInfoUser } from "../helpers/utils";
import { Templates } from "../interfaces/enum";
import { KeyValue } from "../interfaces/general";
import { KeyValue, Locals } from "../interfaces/general";
import { config } from "@anandchowdhary/cosmic";
import { sendNewPassword } from "../rest/auth";
import axios from "axios";
import { getGeolocationFromIp } from "../helpers/location";

/**
* Create a new user
*/
export const createUser = async (user: usersCreateInput) => {
export const createUser = async (user: usersCreateInput, locals?: Locals) => {
user.name = capitalizeFirstAndLastLetter(user.name);
user.password = user.password ? await hash(user.password, 8) : undefined;
if (config("genderPrediction") && !user.gender) {
if (config("registerGenderPrediction") && !user.gender) {
try {
const { data }: { data: { gender: "male" | "female" } } = await axios.get(
`https://api.genderize.io/?name=peter`
Expand All @@ -47,6 +48,17 @@ export const createUser = async (user: usersCreateInput) => {
user.gender = data.gender.toUpperCase() as "MALE" | "FEMALE";
} catch (error) {}
}
if (
locals &&
config("registerLocationDetection") &&
(!user.countryCode || !user.timezone)
) {
try {
const location = await getGeolocationFromIp(locals.ipAddress);
user.countryCode = user.countryCode ?? location?.country_code;
user.timezone = user.timezone ?? location?.time_zone;
} catch (error) {}
}
user.profilePictureUrl =
user.profilePictureUrl ||
`https://api.adorable.io/avatars/285/${createHash("md5")
Expand Down

0 comments on commit e9baebd

Please sign in to comment.