From d49e4b2f8019e8faf3868bd7a64a8319657963a3 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Mon, 10 Oct 2022 12:40:56 +0200 Subject: [PATCH] Added download logs --- .../src/http/download-submissions/index.js | 16 +++++++++++++++- src/api/src/mssql/sql/schema.sql | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/api/src/http/download-submissions/index.js b/src/api/src/http/download-submissions/index.js index 855c606..c73c719 100644 --- a/src/api/src/http/download-submissions/index.js +++ b/src/api/src/http/download-submissions/index.js @@ -147,8 +147,22 @@ export default async ctx => { throw error }) - request.on('done', () => { + request.on('done', async () => { dataStream.push(null) + + // Log the download event + try { + const logger = (await pool.connect()).request() + logger.input('userId', user.info(ctx).id) + logger.input('submissionIds', JSON.stringify(ids)) + logger.input('submissionSearch', search) + + await logger.query(` + insert into DownloadLog (userId, submissionIds, submissionSearch) + values (@userId, @submissionIds, @submissionSearch);`) + } catch (error) { + console.error('Error logging download', error.message) + } }) } catch (error) { console.error(error.message) diff --git a/src/api/src/mssql/sql/schema.sql b/src/api/src/mssql/sql/schema.sql index 3ed603a..5ca303f 100644 --- a/src/api/src/mssql/sql/schema.sql +++ b/src/api/src/mssql/sql/schema.sql @@ -18,6 +18,25 @@ create table Users ( create unique index users_unique_saeonId on Users(saeonId) where saeonId is not null; end +-- Downloads +if not exists ( + select * + from sys.objects + where + object_id = OBJECT_ID(N'[dbo].[DownloadLog]') + and type = 'U' +) +begin +create table DownloadLog ( + id int not null identity primary key, + userId int not null foreign key references Users (id), + timestamp datetime2 not null default GETDATE(), + submissionIds nvarchar(max) null, + submissionSearch nvarchar(max) null, + index ix_DownloadLog_userId nonclustered (userId) +) +end + -- Logins if not exists ( select *