From 5d36bffa2477ce014f7057a7027bdd23835a6f16 Mon Sep 17 00:00:00 2001 From: Mike Rosack Date: Thu, 4 Jan 2024 08:25:12 -0600 Subject: [PATCH] always keep first save --- functions/sns/deleteOldSaves.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/functions/sns/deleteOldSaves.ts b/functions/sns/deleteOldSaves.ts index ad2cf55..3c40dd0 100644 --- a/functions/sns/deleteOldSaves.ts +++ b/functions/sns/deleteOldSaves.ts @@ -3,7 +3,7 @@ import { loggingHandler } from '../../lib/logging'; import { IS3Provider, S3_PROVIDER_SYMBOL } from '../../lib/s3Provider'; import { inject } from '../../lib/ioc'; import { injectable } from 'inversify'; -import { orderBy, take } from 'lodash'; +import { drop, orderBy, take } from 'lodash'; const TURNS_TO_SAVE = 40; @@ -21,12 +21,14 @@ export class DeleteOldSaves { // Make sure we're only looking at the pure save folder!!!111 const resp = await this.s3.listObjects(Config.resourcePrefix + 'saves', `${gameId}/`); - if (resp.Contents.length > TURNS_TO_SAVE) { + if (resp.Contents.length > TURNS_TO_SAVE + 1) { await this.s3.deleteObjects( Config.resourcePrefix + 'saves', - take(orderBy(resp.Contents, ['Key'], ['asc']), resp.Contents.length - TURNS_TO_SAVE).map( - obj => obj.Key - ) + take( + // drop to always keep the first save + drop(orderBy(resp.Contents, ['Key'], ['asc'])), + resp.Contents.length - TURNS_TO_SAVE + ).map(obj => obj.Key) ); } }