Edits history of script submission #376 for ' Trigger when a Notion Database Item has changed (notion)'

  • deno
    import { Resource, getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    import { Client } from "npm:@notionhq/client";
    
    /**
     * See https://developers.notion.com/reference/post-database-query
     */
    export async function main(
      auth: Resource<"notion">,
      database_id: string = d291b756c0914f2984c208924a116cca,
      filter_properties?: string[],
    ) {
      const state = await getState();
      let lastCheck = new Date(state);
      if(!state) {
        //this is the first run, state is not yet set
        lastCheck = new Date();  
        await setState(lastCheck);
      }
    
      const client = new Client({ auth: auth.token });
      if (!filter_properties?.filter(Boolean).length) {
        filter_properties = undefined;
      }
      const entries = await client.databases.query({
        database_id,
        filter_properties,
        filter: {
          "timestamp": "last_edited_time",
          "last_edited_time": {
            "after": lastCheck.toISOString()
          }
        }
      });
    
      const editDates = entries.results.map(r => new Date(r.last_edited_time));
      const lastEntryEdit = new Date(Math.max(...editDates));
      if(editDates.length){
        await setState(lastEntryEdit);
      }
    
      return entries.results;
    }
    

    Submitted by peter kappelt640 1080 days ago