-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3299 from opral:lixdk-322-extend-demo-with-salary…
…-csv Lixdk-322-extend-demo-with-salary-csv
- Loading branch information
Showing
6 changed files
with
271 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
packages/lix-file-manager/src/helper/demo-lix-file/cap-table.csv
This file was deleted.
Oops, something went wrong.
265 changes: 265 additions & 0 deletions
265
packages/lix-file-manager/src/helper/demo-lix-file/demo-lix-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
packages/lix-file-manager/src/helper/demo-lix-file/demoLixFile.ts
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
packages/lix-file-manager/src/helper/demo-lix-file/email-newsletter.csv
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters