Skip to content

Commit fb22592

Browse files
committed
added command to assign role
1 parent 26d8a20 commit fb22592

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ authorization: JWT_TOKEN (returned by Login request)
203203

204204
<br />
205205

206+
## ✨ Update role for existing user
207+
208+
Using npm:
209+
210+
```npm run update-role [user_id] [role_id (optional)]```
211+
212+
Using yarn:
213+
214+
```npm run update-role [user_id] [role_id (optional)]```
215+
216+
- [user_id] is the id of existing user to update role for.
217+
- [role_id] is the id of role: 1 for admin & 2 for user. If you don't provide any role_id it would update user to admin role.
218+
219+
<br />
220+
206221
## ✨ Run the Tests
207222

208223
```yarn test```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"lint": "eslint src --ext .ts",
1818
"build": "tsc -p tsconfig.build.json",
1919
"typecheck": "tsc --noEmit",
20-
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
20+
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
21+
"update-role": "ts-node-dev src/update-role.ts"
2122
},
2223
"dependencies": {
2324
"axios": "^1.2.0",

src/update-role.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import "dotenv/config";
2+
import User from "./models/user";
3+
import { connection, connect } from "./server/database";
4+
const userId = process.argv[2];
5+
const updatedRole = process.argv[3] ?? "1";
6+
7+
const updateUser = async () => {
8+
await connect();
9+
const userRepository = connection!.getRepository(User);
10+
userRepository.find({ id: userId }).then((user: any) => {
11+
if (!user.length) {
12+
console.error( "No user exists with the given id" )
13+
return;
14+
}
15+
const query = { id: user[0].id };
16+
const newValues = { user_role: updatedRole };
17+
userRepository
18+
.update(query, newValues)
19+
.then(() => console.log(`User updated successfully with role ${newValues.user_role}`)
20+
)
21+
.catch((err) => console.error(`error in updating user: ${err.message}`)
22+
);
23+
})
24+
.catch((err) => console.log(`error: ${err.message}`)
25+
)
26+
};
27+
28+
updateUser();

0 commit comments

Comments
 (0)