Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
intrn(manager): make method to check if a role is the only related one
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 16, 2022
1 parent 6350443 commit 4196296
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
21 changes: 11 additions & 10 deletions database/managers/helpers/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import DatabaseError from "$!/errors/database"
import isUndefined from "$/type_guards/is_undefined"
import cleanQuery from "%/managers/helpers/clean_query"

type Column = string
type Literal = ReturnType<typeof literal>
type Where = ReturnType<typeof Sequelize["where"]>

Expand All @@ -32,32 +33,32 @@ export default class Condition<T = any> {
}
}

not(column: string, value: any): Condition {
not(column: Column, value: any): Condition {
this.currentCondition[column] = { [Op.not]: value }
return this
}

is(column: string, value: any): Condition {
is(column: Column, value: any): Condition {
this.currentCondition[column] = { [Op.is]: value }
return this
}

equal(column: string, value: any): Condition {
equal(column: Column, value: any): Condition {
this.currentCondition[column] = { [Op.eq]: value }
return this
}

greaterThanOrEqual(column: string, value: any): Condition {
greaterThanOrEqual(column: Column, value: any): Condition {
this.currentCondition[column] = { [Op.gte]: value }
return this
}

lessThanOrEqual(column: string, value: any): Condition {
lessThanOrEqual(column: Column, value: any): Condition {
this.currentCondition[column] = { [Op.lte]: value }
return this
}

search(column: string, value: string): Condition {
search(column: Column, value: string): Condition {
/*
* `findAndCountAll` uses `findAll`. See
* https://github.com/sequelize/sequelize/blob/0c5ca3fc398a99eddb412fe3b2aba99f157bf59d/src/model.js#L2070
Expand All @@ -72,12 +73,12 @@ export default class Condition<T = any> {
return this
}

isIncludedIn(column: string, value: any[]|Literal): Condition {
isIncludedIn(column: Column, value: any[]|Literal): Condition {
this.currentCondition[column] = { [Op.in]: value }
return this
}

isOnDay(column: string, value: Day): Condition {
isOnDay(column: Column, value: Day): Condition {
let query: any = ""
const escapedColumn = Sequelize.col(column)

Expand Down Expand Up @@ -105,7 +106,7 @@ export default class Condition<T = any> {
return this
}

onOrAfterTime(column: string, time: Time): Condition {
onOrAfterTime(column: Column, time: Time): Condition {
let hourQuery: any = ""
let minuteQuery: any = ""
const escapedColumn = Sequelize.col(column)
Expand Down Expand Up @@ -148,7 +149,7 @@ export default class Condition<T = any> {
)
}

onOrBeforeTime(column: string, time: Time): Condition {
onOrBeforeTime(column: Column, time: Time): Condition {
let hourQuery: any = ""
let minuteQuery: any = ""
const escapedColumn = Sequelize.col(column)
Expand Down
67 changes: 67 additions & 0 deletions database/managers/role.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Op } from "sequelize"

import type { Pipe } from "$/types/database"
import type { Serializable } from "$/types/general"
import type { RoleQueryParameters } from "$/types/query"
Expand Down Expand Up @@ -117,4 +119,69 @@ export default class extends BaseManager<
})
}
}

async isTheOnlyRoleToAnyUser(roleID: number): Promise<boolean> {
try {
if (!Model.sequelize || !AttachedRole.sequelize) {
throw new DatabaseError("Developer may have forgot to register the models.")
}

const subselectQuery = Model.sequelize.literal(`(${
trimRight(
// @ts-ignore
AttachedRole.sequelize.getQueryInterface().queryGenerator.selectQuery(
AttachedRole.tableName, {
"attributes": [ "userID" ],
"where": new Condition().equal(
"roleID",
roleID
)
.build()
}
),
";"
)
})`)
const [ rawUserIDs ] = await AttachedRole.sequelize.query(
// @ts-ignore
AttachedRole.sequelize.getQueryInterface().queryGenerator.selectQuery(
AttachedRole.tableName, {
"attributes": [
"userID",
"roleID"
],
"where": new Condition().equal("roleID", roleID).build()
}
)
) as unknown as [ { id: number, userID: string }[] ]
const userIDs = rawUserIDs.map(info => info.userID)

if (userIDs.length === 0) return false

const [ counts ] = await Model.sequelize.query(
// @ts-ignore
AttachedRole.sequelize.getQueryInterface().queryGenerator.selectQuery(
AttachedRole.tableName, {
"attributes": [
"userID",
[
AttachedRole.sequelize.fn(
"count", "roleID"
),
"roleIDCount"
]
],
"group": [
[ "userID" ]
],
"having": new Condition().equal("roleIDCount", 1).build(),
"where": new Condition().isIncludedIn("userID", userIDs)
}
)
) as unknown as [ { id: number, count: string }[] ]
return counts.length > 0
} catch (error) {
throw this.makeBaseError(error)
}
}
}

0 comments on commit 4196296

Please sign in to comment.