From f38f358fcaa6852298bdf335dafaaf8953c3b13b Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Sat, 5 Dec 2020 12:11:44 -0400 Subject: [PATCH] Debounce upsertUser --- server/src/classes/User.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/server/src/classes/User.js b/server/src/classes/User.js index 7b355b6..0187ccd 100755 --- a/server/src/classes/User.js +++ b/server/src/classes/User.js @@ -1,3 +1,4 @@ +import { addSeconds, differenceInSeconds } from 'date-fns' import { ad } from '../utils/activedirectory' import prisma from '../prisma' import { Change, createClient } from 'ldapjs' @@ -214,9 +215,34 @@ class User { /** * Creates or updates a user in the database + * * @return {Object} */ 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) if (!adUser) throw new Error('Usuário não encontrado')