Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lixdk-322-extend-demo-with-salary-csv #3299

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-falcons-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lix-file-manager": patch
---

replaces newsletter example with salary example that has changes
11 changes: 0 additions & 11 deletions packages/lix-file-manager/src/helper/demo-lix-file/cap-table.csv

This file was deleted.

265 changes: 265 additions & 0 deletions packages/lix-file-manager/src/helper/demo-lix-file/demo-lix-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import {
changeHasLabel,
createAccount,
createChangeSet,
createComment,
createDiscussion,
fileQueueSettled,
Label,
Lix,
newLixFile,
openLixInMemory,
switchAccount,
toBlob,
} from "@lix-js/sdk";
import { plugin as csvPlugin } from "@lix-js/plugin-csv";

export async function lixCsvDemoFile(): Promise<{ blob: Blob; id: string }> {
const lix = await openLixInMemory({
blob: await newLixFile(),
providePlugins: [csvPlugin],
});

const id = await lix.db
.selectFrom("key_value")
.where("key", "=", "lix_id")
.select("value")
.executeTakeFirstOrThrow();

await demoSalariesCsv(lix);

await fileQueueSettled({ lix });
return { blob: await toBlob({ lix }), id: id.value };
}

async function demoSalariesCsv(lix: Lix): Promise<void> {
const anna = await createAccount({
lix,
name: "Anna",
});
const otto = await createAccount({
lix,
name: "Otto",
});
const peter = await createAccount({
lix,
name: "Peter",
});

const rows = [
"Name,Position,Department,Salary",
"John Doe,Software Engineer,Engineering,90000",
"Jane Smith,Product Manager,Product,95000",
"Alice Johnson,Data Scientist,Data,100000",
"Bob Brown,Designer,Design,85000",
"Charlie Davis,Marketing Specialist,Marketing,70000",
"Emily Wilson,HR Manager,HR,75000",
"Frank Miller,Sales Manager,Sales,80000",
"Grace Lee,Customer Support,Support,60000",
"Henry Clark,DevOps Engineer,Engineering,95000",
];

const confirmedLabel = await lix.db
.selectFrom("label")
.where("name", "=", "confirmed")
.selectAll()
.executeTakeFirstOrThrow();

await switchAccount({ lix, to: [anna] });

const file = await lix.db
.insertInto("file")
.values({
path: "/salaries.csv",
data: new TextEncoder().encode(rows.join("\n")),
metadata: {
unique_column: "Name",
},
})
.returning("id")
.executeTakeFirstOrThrow();

// anna is confirming the initial salaries
await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2022-03-11 14:53:00.000",
confirmedLabel,
comment: "Initial salaries",
});

// Otto increases the salary of Charlie Davis
await switchAccount({ lix, to: [otto] });

rows[5] = "Charlie Davis,Marketing Specialist,Marketing,74000";

await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2022-04-14 19:53:00.000",
confirmedLabel,
comment: "Increased Charlie Davis salary",
});

// Peter promotes Alice Johnson
await switchAccount({ lix, to: [peter] });

rows[2] = "Alice Johnson,Senior Data Scientist,Data,110000";

await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2022-05-11 14:53:00.000",
confirmedLabel,
comment: "Promoted Alice Johnson to Senior Data Scientist",
});

// Peter hires a new employee
rows.push("Klaus Kleber,Intern,HR,40000");

const { discussion } = await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2022-05-13 14:53:00.000",
confirmedLabel,
comment: "Hired Klaus Kleber",
});

// Anna thinks the salary is too low of Klaus Kleber
await switchAccount({ lix, to: [anna] });

const annasComment = await createComment({
lix,
parentComment: discussion.firstComment,
content: "I think the salary is too low. Adjust to 45000?",
});

// Otto agrees with Anna
await switchAccount({ lix, to: [otto] });

const ottosComment = await createComment({
lix,
parentComment: annasComment,
content: "I agree. Adjust to 45000.",
});

// Peter agrees with Anna and Otto
await switchAccount({ lix, to: [peter] });

await createComment({
lix,
parentComment: ottosComment,
content: "Aye from me as well",
});

// Anna adjusts the salary
await switchAccount({ lix, to: [anna] });

rows[10] = "Klaus Kleber,Intern,HR,45000";

await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2022-05-14 11:42:00.000",
confirmedLabel,
comment: "Increased Klaus Kleber salary",
});

// Peter promotes Klaus Kleber

await switchAccount({ lix, to: [peter] });

rows[10] = "Klaus Kleber,Junior HR Manager,HR,60000";

await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2023-01-01 10:45:00.000",
confirmedLabel,
comment: "Hired Klaus Kleber after intern period",
});

// Anna updates the salary bands

await switchAccount({ lix, to: [anna] });

// skip header row
for (const row of rows.slice(1)) {
const [name, position, department, salary] = row.split(",");
const salaryInt = parseInt(salary);
rows[rows.indexOf(row)] =
`${name},${position},${department},${(salaryInt * 1.1).toFixed(0)}`;
}

await createAndConfimChanges({
lix,
file,
rows,
timestamp: "2023-02-01 10:45:00.000",
confirmedLabel,
comment: "Updated salary bands",
});
}

async function createAndConfimChanges(args: {
lix: Lix;
file: { id: string };
rows: string[];
timestamp: string;
confirmedLabel: Label;
comment: string;
}) {
await args.lix.db
.updateTable("file")
.set({
data: new TextEncoder().encode(args.rows.join("\n")),
})
.where("id", "=", args.file.id)
.execute();

await fileQueueSettled({ lix: args.lix });

const changes = await args.lix.db
.selectFrom("change")
.selectAll()
// don't copy changes that are already confirmed
.where((eb) => eb.not(changeHasLabel("confirmed")))
.where("file_id", "=", args.file.id)
.execute();

// set the time
for (const change of changes) {
await args.lix.db
.updateTable("change")
.set({ created_at: args.timestamp })
.where("id", "=", change.id)
.execute();
}

const changeSet = await createChangeSet({
lix: args.lix,
changes,
});

const discussion = await createDiscussion({
changeSet,
lix: args.lix,
firstComment: { content: args.comment },
});

await args.lix.db
.insertInto("change_set_label")
.values({
change_set_id: changeSet.id,
label_id: args.confirmedLabel.id,
})
.execute();

return { discussion };
}
50 changes: 0 additions & 50 deletions packages/lix-file-manager/src/helper/demo-lix-file/demoLixFile.ts

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion packages/lix-file-manager/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { atom } from "jotai";
import { plugin as csvPlugin } from "@lix-js/plugin-csv";
import { getOriginPrivateDirectory } from "native-file-system-adapter";
import { lixCsvDemoFile } from "./helper/demo-lix-file/demoLixFile.ts";
import { lixCsvDemoFile } from "./helper/demo-lix-file/demo-lix-file.ts";
import { saveLixToOpfs } from "./helper/saveLixToOpfs.ts";

export const fileIdSearchParamsAtom = atom((get) => {
Expand Down
Loading