-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* routes: return only JSON responses * route documentation for sessions
- Loading branch information
Showing
22 changed files
with
410 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,48 @@ | ||
const mysql = require('./mysql') | ||
const Mysql = require('./mysql') | ||
const Util = require('./util') | ||
|
||
const validate = require('@nictool/nt-validate') | ||
const validate = require('@nictool/validate') | ||
|
||
class Group { | ||
constructor() {} | ||
|
||
async create(args) { | ||
const { error } = validate.group.validate(args) | ||
const { error } = validate.group.v2.validate(args) | ||
if (error) console.error(error) | ||
|
||
const g = await this.get({ nt_group_id: args.nt_group_id }) | ||
if (g.length) { | ||
// console.log(g) | ||
return g[0].nt_group_id | ||
} | ||
if (g.length) return g[0].id | ||
|
||
const groupId = await mysql.insert(`INSERT INTO nt_group`, args) | ||
const groupId = await Mysql.insert(`INSERT INTO nt_group`, args) | ||
return groupId | ||
} | ||
|
||
async get(args) { | ||
return await mysql.select(`SELECT * FROM nt_group WHERE`, args) | ||
args = Util.mapToDbColumn(args, { id: 'nt_group_id' }) | ||
return await Mysql.select( | ||
`SELECT nt_group_id AS id, name FROM nt_group WHERE`, | ||
args, | ||
) | ||
} | ||
|
||
async getAdmin(args) { | ||
return await Mysql.select( | ||
`SELECT nt_group_id AS id | ||
, name | ||
, parent_group_id AS parent_gid | ||
, deleted | ||
FROM nt_group WHERE`, | ||
Util.mapToDbColumn(args, { id: 'nt_group_id' }), | ||
) | ||
} | ||
|
||
async destroy(args) { | ||
const g = await this.get({ nt_group_id: args.nt_group_id }) | ||
// console.log(g) | ||
const g = await this.getAdmin({ nt_group_id: args.nt_group_id }) | ||
if (g.length === 1) { | ||
await mysql.execute(`DELETE FROM nt_group WHERE nt_group_id=?`, [ | ||
g[0].nt_group_id, | ||
]) | ||
await Mysql.execute(`DELETE FROM nt_group WHERE nt_group_id=?`, [g[0].id]) | ||
} | ||
} | ||
} | ||
|
||
module.exports = new Group() | ||
module.exports._mysql = mysql | ||
module.exports._mysql = Mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const assert = require('node:assert/strict') | ||
const { describe, it, after } = require('node:test') | ||
|
||
const session = require('./session') | ||
const userCase = require('../test/user.json') | ||
|
||
after(async () => { | ||
session._mysql.disconnect() | ||
}) | ||
|
||
describe('session', function () { | ||
// session._mysql.debug(true) | ||
let sessionId | ||
|
||
describe('create', () => { | ||
it('creates a login session', async () => { | ||
sessionId = await session.create({ | ||
nt_user_id: userCase.nt_user_id, | ||
nt_user_session: '3.0.0', | ||
}) | ||
assert.ok(sessionId) | ||
}) | ||
}) | ||
|
||
describe('get', () => { | ||
it('finds a session by ID', async () => { | ||
const s = await session.get({ nt_user_session_id: sessionId }) | ||
assert.ok(s.nt_user_session_id) | ||
}) | ||
|
||
it('finds a session by session', async () => { | ||
const s = await session.get({ nt_user_session: '3.0.0' }) | ||
assert.ok(s.nt_user_session_id) | ||
}) | ||
}) | ||
|
||
describe('delete', () => { | ||
it('deletes a session by ID', async () => { | ||
assert.ok(await session.delete({ nt_user_session_id: sessionId })) | ||
}) | ||
|
||
it('does not find a deleted session', async () => { | ||
assert.equal( | ||
await session.get({ nt_user_session_id: sessionId }), | ||
undefined, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.