Skip to content
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
45 changes: 45 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 Down Expand Up @@ -268,6 +270,8 @@ const transformUser = (
user: {
id: unknown;
username: unknown;
firstname: unknown;
lastname: unknown;
public_keys: Array<{
blockchain: string;
public_key: string;
Expand All @@ -279,6 +283,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 +308,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 +337,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 @@ -490,6 +500,41 @@ export async function updateUserAvatar({

return response.update_auth_users;
}
/**
* Update profile of a user
*/

export async function updateUserProfile({
userId,
firstName,
lastName,
}: {
userId: string;
firstName: string | null;
lastName: string | null;
}) {
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;
}

export const getUserByPublicKeyAndChain = async (
publicKey: string,
Expand Down
33 changes: 31 additions & 2 deletions backend/native/backpack-api/src/routes/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getUsersByPublicKeys,
getUsersMetadata,
updateUserAvatar,
updateUserProfile,
} from "../../db/users";
import { getOrcreateXnftSecret } from "../../db/xnftSecrets";
import { logger } from "../../logger";
Expand Down Expand Up @@ -124,7 +125,7 @@ router.get("/jwt/xnft", extractUserId, async (req, res) => {
* Create a new user.
*/
router.post("/", async (req, res) => {
const { username, waitlistId, blockchainPublicKeys } =
const { username, waitlistId, blockchainPublicKeys, firstName, lastName } =
CreateUserWithPublicKeys.parse(req.body);

// Validate all the signatures
Expand Down Expand Up @@ -182,6 +183,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 @@ -247,7 +250,10 @@ router.get("/userById", extractUserId, async (req: Request, res: Response) => {
router.get("/me", extractUserId, async (req: Request, res: Response) => {
if (req.id) {
try {
return res.json({ ...(await getUser(req.id)), jwt: req.jwt });
return res.json({
...(await getUser(req.id)),
jwt: req.jwt,
});
} catch {
// User not found
}
Expand Down Expand Up @@ -374,6 +380,29 @@ router.post("/avatar", extractUserId, async (req: Request, res: Response) => {
return res.status(201).end();
});

/**
* Update profile of the currently authenticated user.
*/

router.post("/profile", extractUserId, async (req: Request, res: Response) => {
if (req.id) {
try {
const resp = await updateUserProfile({
userId: req.id!,
firstName: req.body.firstName,
lastName: req.body.lastName,
});
return res.json({
firstName: resp?.returning[0]?.firstname,
lastName: resp?.returning[0]?.lastname,
});
} catch (error) {
// Couldn't update the user
}
}
return res.status(404).json({ msg: "User not found" });
});

router.post("/metadata", async (req: Request, res: Response) => {
const users = await getUsersMetadata(req.body.uuids);
return res.json({
Expand Down
2 changes: 2 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,8 @@ 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(),
lastName: z.string(),
inviteCode: z
.string()
.regex(
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