@@ -156,11 +156,8 @@ async function render(opts: Options) {
156156 } ;
157157}
158158
159- const dateStr = ( d : Date ) => ( ( d . toISOString ( ) ) . split ( 'T' , 1 ) [ 0 ] ) ;
160-
161- const floorDate = ( d : Date ) => ( new Date ( d . getFullYear ( ) ,
162- d . getMonth ( ) ,
163- d . getDate ( ) ) ) ;
159+ const dateStr =
160+ ( d : Date | string ) => ( ( typeof d === 'string' ? d : d . toISOString ( ) ) . split ( 'T' , 1 ) [ 0 ] ) ;
164161
165162function getSendDate ( c : TemplateContext ) {
166163 let date = c . post . date ;
@@ -175,15 +172,33 @@ function getSendDate(c: TemplateContext) {
175172 return date ;
176173}
177174
175+ function singleSendId ( context : TemplateContext , index ?: SG . SingleSendIndex ) {
176+ if ( ! index )
177+ return undefined ;
178+
179+ const date = dateStr ( context . post . date ) ;
180+
181+ for ( const ss of Object . values ( index . byId ) ) {
182+ if ( dateStr ( ss . send_at ) === date )
183+ return ss . id ;
184+
185+ if ( ss . name . includes ( context . post . title ) )
186+ return ss . id ;
187+ }
188+ }
189+
178190async function run ( options : Options ) {
179191 const { text, context } = await render ( options ) ;
180- // console.log(context);
181192
182193 if ( options . output ) {
183194 await writeFile ( options . output , text ) ;
184195 } else if ( options . apiKey ) {
185196 const sendAt = getSendDate ( context ) ;
186- const id = options . index ?. byDate [ dateStr ( context . post . date ) ] [ 0 ] ;
197+ const id = singleSendId ( context , options . index ) ;
198+
199+ if ( id )
200+ console . log ( `Updating existing Single Send ${ id } ` ) ;
201+
187202 const response = await SG . singleSend ( {
188203 html : text ,
189204 listId : options . listId ,
@@ -219,7 +234,7 @@ type RunOptions = Omit<Options, 'filePath'> & {
219234function dateFilter ( after : number ) {
220235 return ( path : string ) => {
221236 const ctx = contextFromPath ( path ) ;
222- return ctx . date . getTime ( ) > after ;
237+ return ctx . date . getTime ( ) >= after ;
223238 } ;
224239}
225240
@@ -247,7 +262,6 @@ async function runAll(options: RunOptions) {
247262 const index = await SG . indexSingleSends ( { token : options . apiKey } ) ;
248263
249264 for ( const post of posts ) {
250-
251265 const result = await run ( {
252266 ...options ,
253267 filePath : post ,
@@ -276,7 +290,7 @@ async function runAction() {
276290 INPUT_SUBJECT_FORMAT : subject = '%s' ,
277291 INPUT_POSTS_DIR : postsDir ,
278292 INPUT_SLACK_URL : slackUrl ,
279- TODAY_OVERRIDE : today ,
293+ INPUT_AFTER_DATE : today ,
280294 } = process . env ;
281295
282296 if ( ! ( path || postsDir ) ) {
@@ -302,7 +316,6 @@ async function runAction() {
302316}
303317
304318async function testRun ( ) {
305- // const apiKey = 'REAS-yuff0naum!krar';
306319 process . env [ 'INPUT_SENDGRID_LIST_ID' ] = "559adb5e-7164-4ac8-bbb5-1398d4ff0df9" ;
307320 // process.env['INPUT_SENDGRID_API_KEY'] = apiKey;
308321 // process.env['INPUT_TEXT_PATH'] = __dirname + '/../../../../_posts/2021-11-16-communications-lead.md';
@@ -311,8 +324,6 @@ async function testRun() {
311324 process . env [ 'INPUT_CONTEXT' ] = `{}` ;
312325 process . env [ 'INPUT_SUPPRESSION_GROUP_ID' ] = '17889' ;
313326 process . env [ 'INPUT_SITE_YAML' ] = __dirname + '/../../../../_config.yml' ;
314- process . env [ 'INPUT_SLACK_URL' ] = 'https://hooks.slack.com/services/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' ;
315- process . env [ 'TODAY_OVERRIDE' ] = '2022-01-10' ;
316327
317328 await runAction ( ) ;
318329}
0 commit comments