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

Feat: Mutate Firstname and Lastname of user in Your Account under Settings #28

Open
wants to merge 8 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
3 changes: 2 additions & 1 deletion backend/native/backpack-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"express": "^4.18.2",
"express-rate-limit": "^6.7.0",
"graphql": "^16.6.0",
"http-proxy-middleware": "^2.0.6",
"http_ece": "^1.1.0",
"http-proxy-middleware": "^2.0.6",
"jose": "^4.11.1",
"jsbi": "^4.3.0",
"jsonwebtoken": "^8.5.1",
Expand All @@ -49,6 +49,7 @@
"zod": "^3.19.1"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.4",
"eslint-config-custom": "*"
},
"scripts": {
Expand Down
66 changes: 63 additions & 3 deletions backend/native/backpack-api/src/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export const getUsers = async (
{
id: true,
username: true,
firstname: true,
lastname: true,
},
],
auth_public_keys: [
Expand Down Expand Up @@ -113,6 +115,8 @@ export const getUsers = async (
(userResponse) => ({
id: userResponse.id,
username: userResponse.username,
firstname: userResponse.firstname,
lastname: userResponse.lastname,
public_keys:
userToPublicKeyMapping[userResponse.id as string].map((x) => ({
blockchain: x.blockchain,
Expand Down Expand Up @@ -212,6 +216,8 @@ export const getUser = async (id: string, onlyActiveKeys?: boolean) => {
{
id: true,
username: true,
firstname: true,
lastname: true,
public_keys: [
{},
{
Expand Down Expand Up @@ -251,6 +257,8 @@ const transformUsers = (
users: {
id: unknown;
username: unknown;
firstname: unknown;
lastname: unknown;
public_keys: Array<{
blockchain: string;
public_key: string;
Expand All @@ -268,6 +276,8 @@ const transformUser = (
user: {
id: unknown;
username: unknown;
firstname: unknown;
lastname: unknown;
public_keys: Array<{
blockchain: string;
public_key: string;
Expand All @@ -279,6 +289,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,12 +314,16 @@ const transformUser = (
*/
export const createUser = async (
username: string,
firstname: string,
lastname: string,
blockchainPublicKeys: Array<{ blockchain: Blockchain; publicKey: string }>,
waitlistId?: string | null,
referrerId?: string
): Promise<{
id: string;
username: string;
firstname: string;
lastname: string;
public_keys: { blockchain: "solana" | "ethereum"; id: number }[];
}> => {
const inviteCode = uuidv4();
Expand All @@ -323,14 +339,15 @@ export const createUser = async (
},
],
});

const response = await chain("mutation")({
insert_auth_users_one: [
{
object: {
username: username,
firstname: firstname,
lastname: lastname,
public_keys: {
data: blockchainPublicKeys.map((b) => ({
data: blockchainPublicKeys?.map((b) => ({
blockchain: b.blockchain,
public_key: b.publicKey,
})),
Expand All @@ -343,6 +360,8 @@ export const createUser = async (
{
id: true,
username: true,
firstname: true,
lastname: true,
public_keys: [
{},
{
Expand All @@ -355,7 +374,6 @@ export const createUser = async (
},
],
});

// @ts-ignore
return response.insert_auth_users_one;
};
Expand Down Expand Up @@ -491,6 +509,48 @@ export async function updateUserAvatar({
return response.update_auth_users;
}

/**
* Update User firstname and lastname
*/
export const updateUserName = async ({
firstname,
lastname,
userId
}: {
firstname: string,
lastname: string,
userId: string
}): Promise<
{
id: string;
firstname: string;
lastname: string;
}
> => {
const response = await chain("mutation")({
update_auth_users: [
{
where: {
id: { _eq: userId },
},
_set: {
firstname: firstname,
lastname: lastname
},
},
{
returning: {
firstname: true,
id: true,
lastname: true
}
},
],
});


return response.update_auth_users;
}
export const getUserByPublicKeyAndChain = async (
publicKey: string,
blockchain: Blockchain
Expand Down
1 change: 1 addition & 0 deletions backend/native/backpack-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ app.get("/", (_req, res) => {
uptime: process.uptime(),
message: "OK",
timestamp: Date.now(),
hello: 'From here,'
});
});

Expand Down
4 changes: 2 additions & 2 deletions backend/native/backpack-api/src/routes/v1/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ router.put("/message", extractUserId, ensureHasRoomAccess, async (req, res) => {
if (state !== "cancelled" && state !== "redeemed") {
return res.status(411).json({ msg: "Incorrect state" });
}
await updateSecureTransfer(messageId, room, state, txn);
await updateSecureTransfer(messageId, room as string, state, txn);
res.json({});
});

Expand All @@ -72,7 +72,7 @@ router.get("/", extractUserId, ensureHasRoomAccess, async (req, res) => {
? // @ts-ignore
new Date(parseInt(req.query.timestampAfter))
: new Date(0);
const limit = req.query.limit ? parseInt(req.query.limit) : 10;
const limit = req.query.limit ? parseInt(req.query.limit as string) : 10;
// @ts-ignore
const clientGeneratedUuid: string | undefined = req.query.clientGeneratedUuid;

Expand Down
31 changes: 28 additions & 3 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,
updateUserName,
} from "../../db/users";
import { getOrcreateXnftSecret } from "../../db/xnftSecrets";
import { logger } from "../../logger";
Expand Down Expand Up @@ -124,7 +125,8 @@ 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,15 +184,18 @@ router.post("/", async (req, res) => {

const user = await createUser(
username,
firstname,
lastname,
blockchainPublicKeys.map((b) => ({
...b,
// Cast blockchain to correct type
blockchain: b.blockchain as Blockchain,
})),
waitlistId,
referrerId
);

);


user?.public_keys.map(async ({ blockchain, id }) => {
//TODO: make a bulk, single call here
await updatePublicKey({
Expand Down Expand Up @@ -247,6 +252,8 @@ router.get("/userById", extractUserId, async (req: Request, res: Response) => {
router.get("/me", extractUserId, async (req: Request, res: Response) => {
if (req.id) {
try {
const user = await getUser(req.id);
console.log(user);
return res.json({ ...(await getUser(req.id)), jwt: req.jwt });
} catch {
// User not found
Expand Down Expand Up @@ -414,4 +421,22 @@ router.post(
}
);

/**
* Update the firstname and lastname of the user
*/
router.post(
"/updateName",
extractUserId,
async (req: Request, res: Response) => {
const response = await updateUserName({
userId: req.id!,
firstname: req.body.firstname,
lastname: req.body.lastname
});

return res.status(201).json(response).end();

}
)

export default router;
4 changes: 4 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,10 @@ 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
table:
name: users
schema: auth
configuration:
column_config:
firstname:
custom_name: firstname
lastname:
custom_name: lastname
custom_column_names:
firstname: firstname
lastname: lastname
custom_root_fields: {}
object_relationships:
- name: invitation
using:
Expand Down Expand Up @@ -65,7 +75,9 @@ insert_permissions:
permission:
check: {}
columns:
- firstname
- invitation_id
- lastname
- referrer_id
- username
- waitlist_id
Expand All @@ -82,7 +94,9 @@ select_permissions:
permission:
columns:
- created_at
- firstname
- id
- lastname
- username
filter: {}
allow_aggregations: true
Expand Down Expand Up @@ -110,6 +124,8 @@ update_permissions:
permission:
columns:
- avatar_nft
- firstname
- lastname
- updated_at
filter: {}
check: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "auth"."users" add column "firstname" citext
-- not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "auth"."users" add column "firstname" citext
not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "auth"."users" add column "lastname" citext
-- not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "auth"."users" add column "lastname" citext
not null;
Loading