11const crypto = require ( 'node:crypto' )
2- const validate = require ( '@nictool/validate' )
32
43const Mysql = require ( './mysql' )
54const Util = require ( './util' )
65Util . setEnv ( )
76const Config = require ( './config' )
87
8+ const userDbMap = { id : 'nt_user_id' , gid : 'nt_group_id' }
9+
910class User {
1011 constructor ( args ) {
1112 this . debug = args ?. debug ?? false
@@ -61,24 +62,19 @@ class User {
6162 }
6263
6364 async create ( args ) {
64- const { error } = validate . user . v2 . validate ( args )
65- if ( error ) console . error ( error )
66-
67- const u = await this . get ( {
68- nt_user_id : args . nt_user_id ,
69- nt_group_id : args . nt_group_id ,
70- } )
71- if ( u . length ) {
72- // console.log(u)
73- return u [ 0 ] . nt_user_id
74- }
65+ // console.log(args)
66+ const u = await this . get ( { id : args . id , gid : args . gid } )
67+ if ( u . length === 1 ) return u [ 0 ] . id
7568
7669 if ( args . password ) {
7770 if ( ! args . pass_salt ) args . pass_salt = this . generateSalt ( )
7871 args . password = await this . hashAuthPbkdf2 ( args . password , args . pass_salt )
7972 }
8073
81- const userId = await Mysql . insert ( `INSERT INTO nt_user` , args )
74+ const userId = await Mysql . insert (
75+ `INSERT INTO nt_user` ,
76+ Util . mapToDbColumn ( args , userDbMap ) ,
77+ )
8278 return userId
8379 }
8480
@@ -92,7 +88,7 @@ class User {
9288 , username
9389 , email
9490 FROM nt_user WHERE` ,
95- Util . mapToDbColumn ( args , { id : 'nt_user_id' , gid : 'nt_group_id' } ) ,
91+ Util . mapToDbColumn ( args , userDbMap ) ,
9692 )
9793 }
9894
@@ -108,22 +104,22 @@ class User {
108104 , email
109105 , deleted
110106 FROM nt_user WHERE` ,
111- Util . mapToDbColumn ( args , { id : 'nt_user_id' , gid : 'nt_group_id' } ) ,
107+ Util . mapToDbColumn ( args , userDbMap ) ,
112108 )
113109 }
114110
115111 async delete ( args , val ) {
116- const u = await this . getAdmin ( { nt_user_id : args . nt_user_id } )
117- if ( u . length === 1 ) {
118- await Mysql . execute ( `UPDATE nt_user SET deleted=? WHERE nt_user_id=?` , [
119- val ?? 1 ,
120- u [ 0 ] . id ,
121- ] )
122- }
112+ const u = await this . getAdmin ( args )
113+ if ( u . length !== 1 ) return false
114+ await Mysql . execute ( `UPDATE nt_user SET deleted=? WHERE nt_user_id=?` , [
115+ val ?? 1 ,
116+ u [ 0 ] . id ,
117+ ] )
118+ return true
123119 }
124120
125121 async destroy ( args ) {
126- const u = await this . getAdmin ( { nt_user_id : args . nt_user_id } )
122+ const u = await this . getAdmin ( args )
127123 if ( u . length === 1 ) {
128124 await Mysql . execute ( `DELETE FROM nt_user WHERE nt_user_id=?` , [ u [ 0 ] . id ] )
129125 }
0 commit comments