Debounce upsertUser
This commit is contained in:
parent
05db879b27
commit
f38f358fca
|
@ -1,3 +1,4 @@
|
||||||
|
import { addSeconds, differenceInSeconds } from 'date-fns'
|
||||||
import { ad } from '../utils/activedirectory'
|
import { ad } from '../utils/activedirectory'
|
||||||
import prisma from '../prisma'
|
import prisma from '../prisma'
|
||||||
import { Change, createClient } from 'ldapjs'
|
import { Change, createClient } from 'ldapjs'
|
||||||
|
@ -214,9 +215,34 @@ class User {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates or updates a user in the database
|
* Creates or updates a user in the database
|
||||||
|
*
|
||||||
* @return {Object}
|
* @return {Object}
|
||||||
*/
|
*/
|
||||||
static async upsertUser(username) {
|
static async upsertUser(username) {
|
||||||
|
const DEBOUNCE_TIME_IN_SECONDS = 350
|
||||||
|
|
||||||
|
const oldUserData = await prisma.user.findUnique({
|
||||||
|
where: { sAMAccountName: username }
|
||||||
|
})
|
||||||
|
|
||||||
|
const now = new Date()
|
||||||
|
|
||||||
|
if (
|
||||||
|
oldUserData &&
|
||||||
|
differenceInSeconds(
|
||||||
|
now,
|
||||||
|
addSeconds(oldUserData.updatedAt, DEBOUNCE_TIME_IN_SECONDS)
|
||||||
|
) < 0
|
||||||
|
)
|
||||||
|
return oldUserData
|
||||||
|
|
||||||
|
await prisma.user.update({
|
||||||
|
data: { updatedAt: now },
|
||||||
|
where: { sAMAccountName: username }
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('updating')
|
||||||
|
|
||||||
const adUser = await ad.findUser(username)
|
const adUser = await ad.findUser(username)
|
||||||
if (!adUser) throw new Error('Usuário não encontrado')
|
if (!adUser) throw new Error('Usuário não encontrado')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user