Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firstname and Lastname #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions backend/native/backpack-api/src/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ export const getUser = async (id: string, onlyActiveKeys?: boolean) => {
{
id: true,
username: true,
firstname: true,
lastname: true,
public_keys: [
{},
{
Expand All @@ -229,6 +231,14 @@ export const getUser = async (id: string, onlyActiveKeys?: boolean) => {
}
return transformUser(response.auth_users_by_pk, onlyActiveKeys);
};
/**
* update user
*/
// export const updateUser = async (userId: string) => {
// const res = chain("mutation",{
// update_auth_users_one: []
// })
// }

export const getReferrer = async (userId: string) => {
const { auth_users_by_pk } = await chain("query")({
Expand Down Expand Up @@ -268,6 +278,8 @@ const transformUser = (
user: {
id: unknown;
username: unknown;
firstname: unknown;
lastname: unknown;
public_keys: Array<{
blockchain: string;
public_key: string;
Expand All @@ -279,6 +291,8 @@ const transformUser = (
return {
id: user.id,
username: user.username,
firstname: user.firstname,
lastname: user.lastname,
// Camelcase public keys for response
publicKeys: user.public_keys
.map((k) => ({
Expand All @@ -302,6 +316,8 @@ const transformUser = (
*/
export const createUser = async (
username: string,
firstname: string,
lastname: string,
blockchainPublicKeys: Array<{ blockchain: Blockchain; publicKey: string }>,
waitlistId?: string | null,
referrerId?: string
Expand Down Expand Up @@ -329,6 +345,8 @@ export const createUser = async (
{
object: {
username: username,
firstname: firstname,
lastname: lastname,
public_keys: {
data: blockchainPublicKeys.map((b) => ({
blockchain: b.blockchain,
Expand Down Expand Up @@ -360,6 +378,42 @@ export const createUser = async (
return response.insert_auth_users_one;
};

/**
* update user
*/

export const updateUserProfile = async ({
firstname,
lastname,
userId,
}: {
firstname: string | null;
lastname: string | null;
userId: string;
}) => {
const response = await chain("mutation")({
update_auth_users: [
{
where: {
id: { _eq: userId },
},
_set: {
firstname: firstname,
lastname: lastname,
},
},
{
returning: {
firstname: true,
lastname: true,
},
},
],
});

return response.update_auth_users;
};

/**
* Search for users by prefix.
*/
Expand Down
38 changes: 37 additions & 1 deletion backend/native/backpack-api/src/routes/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getAddMessage,
getCreateMessage,
} from "@coral-xyz/common";
import { id } from "ethers/lib/utils";
import type { Request, Response } from "express";
import express from "express";
import rateLimit from "express-rate-limit";
Expand All @@ -31,6 +32,7 @@ import {
getUsersByPublicKeys,
getUsersMetadata,
updateUserAvatar,
updateUserProfile,
} from "../../db/users";
import { getOrcreateXnftSecret } from "../../db/xnftSecrets";
import { logger } from "../../logger";
Expand Down Expand Up @@ -110,6 +112,26 @@ router.get("/", extractUserId, async (req, res) => {
});
});

router.post("/profile", extractUserId, async (req: Request, res: Response) => {
if (req.id) {
try {
const response = await updateUserProfile({
userId: req.id,
firstname: req.body.firstname,
lastname: req.body.lastname,
});
console.log(response);
return res.json({
firstname: response,
});
} catch (e) {
// not able to update user
}
} else {
res.status(404).json({ msg: "User not found" });
}
});

router.get("/jwt/xnft", extractUserId, async (req, res) => {
// @ts-ignore
const uuid = req.id as string;
Expand All @@ -124,9 +146,11 @@ router.get("/jwt/xnft", extractUserId, async (req, res) => {
* Create a new user.
*/
router.post("/", async (req, res) => {
const { username, waitlistId, blockchainPublicKeys } =
const { username, firstname, lastname, waitlistId, blockchainPublicKeys } =
CreateUserWithPublicKeys.parse(req.body);

console.log(firstname);
console.log(lastname);
// Validate all the signatures
for (const blockchainPublicKey of blockchainPublicKeys) {
const signedMessage = getCreateMessage(blockchainPublicKey.publicKey);
Expand Down Expand Up @@ -182,6 +206,8 @@ router.post("/", async (req, res) => {

const user = await createUser(
username,
firstname,
lastname,
blockchainPublicKeys.map((b) => ({
...b,
// Cast blockchain to correct type
Expand Down Expand Up @@ -231,6 +257,16 @@ router.post("/", async (req, res) => {
return res.json({ id: user.id, msg: "ok", jwt });
});

/**
* Update user
*/
// router.post("/me", extractUserId, async (req: Request, res: Response ) => {
// if(req.id){
// try {

// }
// }
// })
/**
* Fetches User detail by id
*/
Expand Down
12 changes: 12 additions & 0 deletions backend/native/backpack-api/src/validation/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export const BaseCreateUser = z.object({
/^[a-z0-9_]{3,15}$/,
"should be between 3-15 characters and can only contain numbers, letters, and underscores."
),
firstname: z
.string()
.regex(
/^[a-z0-9_]{3,15}$/,
"should be between 3-15 characters and can only contain numbers, letters, and underscores."
),
lastname: z
.string()
.regex(
/^[a-z0-9_]{3,15}$/,
"should be between 3-15 characters and can only contain numbers, letters, and underscores."
),
inviteCode: z
.string()
.regex(
Expand Down
1 change: 1 addition & 0 deletions backend/native/zeus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"graphql": "^16.8.1",
"tsc-alias": "^1.7.1",
"typescript": "~4.9.3"
},
Expand Down
14 changes: 14 additions & 0 deletions backend/native/zeus/src/zeus/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,10 @@ export const AllTypesProps: Record<string, any> = {
_or: "auth_users_bool_exp",
created_at: "timestamptz_comparison_exp",
dropzone_public_key: "auth_public_keys_bool_exp",
firstname: "String_comparison_exp",
id: "uuid_comparison_exp",
invitation: "auth_invitations_bool_exp",
lastname: "String_comparison_exp",
public_keys: "auth_public_keys_bool_exp",
public_keys_aggregate: "auth_public_keys_aggregate_bool_exp",
referred_users: "auth_users_bool_exp",
Expand All @@ -784,12 +786,16 @@ export const AllTypesProps: Record<string, any> = {
},
auth_users_max_order_by: {
created_at: "order_by",
firstname: "order_by",
id: "order_by",
lastname: "order_by",
username: "order_by",
},
auth_users_min_order_by: {
created_at: "order_by",
firstname: "order_by",
id: "order_by",
lastname: "order_by",
username: "order_by",
},
auth_users_obj_rel_insert_input: {
Expand All @@ -804,8 +810,10 @@ export const AllTypesProps: Record<string, any> = {
auth_users_order_by: {
created_at: "order_by",
dropzone_public_key_aggregate: "auth_public_keys_aggregate_order_by",
firstname: "order_by",
id: "order_by",
invitation: "auth_invitations_order_by",
lastname: "order_by",
public_keys_aggregate: "auth_public_keys_aggregate_order_by",
referred_users_aggregate: "auth_users_aggregate_order_by",
referrer: "auth_users_order_by",
Expand Down Expand Up @@ -2084,8 +2092,10 @@ export const ReturnTypes: Record<string, any> = {
auth_users: {
created_at: "timestamptz",
dropzone_public_key: "auth_public_keys",
firstname: "String",
id: "uuid",
invitation: "auth_invitations",
lastname: "String",
public_keys: "auth_public_keys",
public_keys_aggregate: "auth_public_keys_aggregate",
referred_users: "auth_users",
Expand All @@ -2104,12 +2114,16 @@ export const ReturnTypes: Record<string, any> = {
},
auth_users_max_fields: {
created_at: "timestamptz",
firstname: "String",
id: "uuid",
lastname: "String",
username: "citext",
},
auth_users_min_fields: {
created_at: "timestamptz",
firstname: "String",
id: "uuid",
lastname: "String",
username: "citext",
},
auth_users_mutation_response: {
Expand Down
Loading